Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions dart/length_of_last_word.dart
Original file line number Diff line number Diff line change
@@ -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;
}
}