Given a string s consisting of words and spaces, return the length of the last word in the string.
A word is a maximal substring consisting of non-space characters only.
The last word is "World" with length 5.
The last word is "moon" with length 4.
The last word is "joyboy" with length 6.
1 <= s.length <= 10^4s consists of only English letters and spaces ' '.s.No developer has claimed the Python throne for LeetCode #58 yet. Submit an optimal idiomatic solution to become the reigning king and display your portfolio dofollow backlink to visiting engineers worldwide.
split(), last(), and len().index.Click to view the answer
You can solve this by traversing the string backwards. There are only two cases to consider: whether the current character is a space or not.
length.length > 0, it means you have already encountered letters. At this point, if the current character is a space, you can return the result.
class Solution:
def lengthOfLastWord(self, s: str) -> int:
length = 0
for i in range(len(s) - 1, -1, -1):
if s[i] == " ":
if length > 0:
return length
continue
length += 1
return length
No developer has claimed the Ruby throne for LeetCode #58 yet. Submit an optimal idiomatic solution to become the reigning king and display your portfolio dofollow backlink to visiting engineers worldwide.
split(), last(), and len().index.Click to view the answer
You can solve this by traversing the string backwards. There are only two cases to consider: whether the current character is a space or not.
length.length > 0, it means you have already encountered letters. At this point, if the current character is a space, you can return the result.
# @param {String} s
# @return {Integer}
def length_of_last_word(s)
length = 0
(s.size - 1).downto(0) do |i|
if s[i] == " "
return length if length > 0
next
end
length += 1
end
length
end
No developer has claimed the Java throne for LeetCode #58 yet. Submit an optimal idiomatic solution to become the reigning king and display your portfolio dofollow backlink to visiting engineers worldwide.
split(), last(), and len().index.Click to view the answer
You can solve this by traversing the string backwards. There are only two cases to consider: whether the current character is a space or not.
length.length > 0, it means you have already encountered letters. At this point, if the current character is a space, you can return the result.
class Solution {
public int lengthOfLastWord(String s) {
int length = 0;
for (int i = s.length() - 1; i >= 0; i--) {
if (s.charAt(i) == ' ') {
if (length > 0) {
return length;
}
continue;
}
length++;
}
return length;
}
}
No developer has claimed the C++ throne for LeetCode #58 yet. Submit an optimal idiomatic solution to become the reigning king and display your portfolio dofollow backlink to visiting engineers worldwide.
split(), last(), and len().index.Click to view the answer
You can solve this by traversing the string backwards. There are only two cases to consider: whether the current character is a space or not.
length.length > 0, it means you have already encountered letters. At this point, if the current character is a space, you can return the result.
class Solution {
public:
int lengthOfLastWord(string s) {
int length = 0;
int n = s.size();
for (int i = n - 1; i >= 0; i--) {
if (s[i] == ' ') {
if (length > 0) {
return length;
}
continue;
}
length++;
}
return length;
}
};
No developer has claimed the C# throne for LeetCode #58 yet. Submit an optimal idiomatic solution to become the reigning king and display your portfolio dofollow backlink to visiting engineers worldwide.
split(), last(), and len().index.Click to view the answer
You can solve this by traversing the string backwards. There are only two cases to consider: whether the current character is a space or not.
length.length > 0, it means you have already encountered letters. At this point, if the current character is a space, you can return the result.
public class Solution {
public int LengthOfLastWord(string s) {
int length = 0;
for (int i = s.Length - 1; i >= 0; i--) {
if (s[i] == ' ') {
if (length > 0) {
return length;
}
continue;
}
length++;
}
return length;
}
}
No developer has claimed the Javascript throne for LeetCode #58 yet. Submit an optimal idiomatic solution to become the reigning king and display your portfolio dofollow backlink to visiting engineers worldwide.
split(), last(), and len().index.Click to view the answer
You can solve this by traversing the string backwards. There are only two cases to consider: whether the current character is a space or not.
length.length > 0, it means you have already encountered letters. At this point, if the current character is a space, you can return the result.
/**
* @param {string} s
* @return {number}
*/
var lengthOfLastWord = function (s) {
let length = 0;
for (let i = s.length - 1; i >= 0; i--) {
if (s[i] === " ") {
if (length > 0) {
return length;
}
continue;
}
length++;
}
return length;
};
No developer has claimed the Go throne for LeetCode #58 yet. Submit an optimal idiomatic solution to become the reigning king and display your portfolio dofollow backlink to visiting engineers worldwide.
split(), last(), and len().index.Click to view the answer
You can solve this by traversing the string backwards. There are only two cases to consider: whether the current character is a space or not.
length.length > 0, it means you have already encountered letters. At this point, if the current character is a space, you can return the result.
func lengthOfLastWord(s string) int {
length := 0
for i := len(s) - 1; i >= 0; i-- {
if s[i] == ' ' {
if length > 0 {
return length
}
continue
}
length++
}
return length
}
No developer has claimed the Rust throne for LeetCode #58 yet. Submit an optimal idiomatic solution to become the reigning king and display your portfolio dofollow backlink to visiting engineers worldwide.
No developer has claimed the Swift throne for LeetCode #58 yet. Submit an optimal idiomatic solution to become the reigning king and display your portfolio dofollow backlink to visiting engineers worldwide.
No developer has claimed the Kotlin throne for LeetCode #58 yet. Submit an optimal idiomatic solution to become the reigning king and display your portfolio dofollow backlink to visiting engineers worldwide.