Simple 2-Liner C++ Solution


  • 0
    M
    class Solution {
    public:
        int lengthOfLastWord(string s) {
            while (!s.empty() && s.back() == ' ') s.pop_back();
            return s.size() - (s.rfind(' ') + 1);
        }
    };
    

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.