LeetCode - Algorithms - 58. Length of Last Word

Problem

58. Length of Last Word

Java

jdk

1
2
3
4
5
6
7
8
9
10
class Solution {
public int lengthOfLastWord(String s) {
int len = 0;
s = s.trim();
String w = s.substring(s.lastIndexOf(" ") + 1);
if (w != null && !w.isEmpty())
len = w.length();
return len;
}
}

Submission Detail

  • 58 / 58 test cases passed.
  • Runtime: 0 ms, faster than 100.00% of Java online submissions for Length of Last Word.
  • Memory Usage: 37.3 MB, less than 49.80% of Java online submissions for Length of Last Word.