Problem Statement:
Given a string s consisting of words and spaces, return the length of the last word in the string.
A word is defined as a maximal substring consisting of non-space characters only.
Example:
Input: s = “Hello World”
Output: 5
Input: s = ” fly me to the moon “
Output: 4
Input: s = “luffy is still joyboy”
Output: 6
Constraints:
- 1 ≤ s.length ≤ 104
- s consists of only English letters and spaces
' ' - There will be at least one word in
s
Approach:
- Start from the end and skip trailing spaces.
- Count characters until the next space or beginning of string.
- This gives the length of the last word efficiently.
Time and Space Complexity:
- Time Complexity: O(n)
- Space Complexity: O(1)
var lengthOfLastWord = function(s) {
let n = s.length - 1;
let count = 0;
while (n >= 0) {
if (s[n] !== " ") {
count++;
} else if (count > 0) {
break;
}
n--;
}
return count;
};
int lengthOfLastWord(char* s) {
int len = strlen(s);
int i = len - 1, count = 0;
while (i >= 0) {
if (s[i] != ' ') {
count++;
} else if (count > 0) {
break;
}
i--;
}
return count;
}
class Solution {
public:
int lengthOfLastWord(string s) {
int i = s.length() - 1, count = 0;
while (i >= 0) {
if (s[i] != ' ') {
count++;
} else if (count > 0) {
break;
}
i--;
}
return count;
}
};
class Solution {
public int lengthOfLastWord(String s) {
int i = s.length() - 1, count = 0;
while (i >= 0) {
if (s.charAt(i) != ' ') {
count++;
} else if (count > 0) {
break;
}
i--;
}
return count;
}
}
class Solution(object):
def lengthOfLastWord(self, s):
i = len(s) - 1
count = 0
while i >= 0:
if s[i] != ' ':
count += 1
elif count > 0:
break
i -= 1
return count
public class Solution {
public int LengthOfLastWord(string s) {
int i = s.Length - 1, count = 0;
while (i >= 0) {
if (s[i] != ' ') {
count++;
} else if (count > 0) {
break;
}
i--;
}
return count;
}
}
