10 lines 5 ms solution in JAVA


  • 0
    H
        public int lengthOfLastWord(String s) {
            int count = 0;
            for(int i = s.length() - 1;i >= 0;i--){
                if(s.charAt(i) != ' ') count++;
                if(s.charAt(i) == ' ' && count != 0) break;
            }
            return count;
        }
    

Log in to reply
 

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