A java solution


  • 0
    P
    class Solution {
        public int lengthOfLastWord(String s) {
            if(s == ""){
                return 0;
            }
            String[] words = s.split("\\s");
            int length = words.length;
            if(length == 0){
                return 0;
            }
            String result = words[length - 1];
            return result.length();
        }
    }
    

Log in to reply
 

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