diff --git a/dart/length_of_last_word.dart b/dart/length_of_last_word.dart new file mode 100644 index 0000000..e7d0869 --- /dev/null +++ b/dart/length_of_last_word.dart @@ -0,0 +1,23 @@ +/* +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. +*/ + +class Solution { + int lengthOfLastWord(String s) { + int index = s.length - 1; + int length = 0; + + while (index >= 0 && s[index] == ' ') { + index--; + } + + while (index >= 0 && s[index] != ' ') { + length++; + index--; + } + + return length; + } +}