Implement lengthOfLastWord function in Solution class#270
Closed
aryaman0406 wants to merge 1 commit intoSjxSubham:mainfrom
Closed
Implement lengthOfLastWord function in Solution class#270aryaman0406 wants to merge 1 commit intoSjxSubham:mainfrom
aryaman0406 wants to merge 1 commit intoSjxSubham:mainfrom
Conversation
Reviewer's guide (collapsed on small PRs)Reviewer's GuideAdds a C++ implementation of lengthOfLastWord in the Solution class by performing a two-pass backward traversal to skip trailing spaces and then count the last word’s characters, achieving O(N) time and O(1) space without extra allocations. File-Level Changes
Tips and commandsInteracting with Sourcery
Customizing Your ExperienceAccess your dashboard to:
Getting Help
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
PR Title Format: 58.Length of Last Word.cpp💡 IntuitionThe most straightforward way to find the length of the last word is to iterate through the string backward from the end. This allows us to skip any trailing spaces immediately and then count the characters of the first non-space sequence we encounter.✍️ ApproachThe solution uses a two-stage backward iteration approach:Skip Trailing Spaces: Start the index i at the end of the string ($\text{s.length() - 1}$ ). Use a while loop to decrement i as long as the character $\text{s[i]}$ is a space (' ') and $\text{i}$ is within the string bounds. This ensures we stop at the last character of the actual last word or at the beginning of the string.Count Word Characters: Initialize a counter $\text{length}$ to $0$ . Use a second while loop, again decrementing $\text{i}$ , but this time, stop when $\text{s[i]}$ is a space or $\text{i}$ goes below $0$ . In this loop, simply increment $\text{length}$ for every non-space character encountered.Return: The final value of $\text{length}$ is the length of the last word. This approach avoids creating any new strings or using built-in string splitting functions, making it efficient with $O(N)$ time complexity and $O(1)$ space complexity.Code Solution (C++)C++class Solution {
public:
int lengthOfLastWord(string s) {
int length = 0;
// Start from the end of the string
int i = s.length() - 1;
};
🔗 Related IssuesBy submitting this PR, I confirm that:[x] This is my original work not totally AI generated[x] I have tested the solution thoroughly on leetcode[x] I have maintained proper PR description format[x] This is a meaningful contribution, not spam
Summary by Sourcery
New Features: