Simple java code (3 lines)


  • -1
    M
    public int lengthOfLastWord(String s) {
        if (s.length() == 0 || s.split("\\s").length == 0)
    		return 0;
    	return s.split("\\s")[s.split("\\s").length - 1].length();
    }

Log in to reply
 

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