diff --git a/58_length_of_last_word/main.cpp b/58_length_of_last_word/main.cpp new file mode 100644 index 0000000..b16ee85 --- /dev/null +++ b/58_length_of_last_word/main.cpp @@ -0,0 +1,14 @@ +#include +using namespace std; + +class Solution { + public: + int lengthOfLastWord(string s) { + int i = s.length() - 1, result = 0; + + while (s[i] == ' ') i--; + for (; i >= 0 && s[i] != ' '; i--, result++); + + return result; + } +};