Fastest java solution for this problem


  • 0
    Y

    This is probably one of the fastest solutions (at least by the ms graph) :

    public class Solution {
        public int lengthOfLastWord(String s) {
            if (s.equals("") || s==null)
                return 0;
            int count = 0;
            for (int i=s.length()-1; i>=0; i--) {
                if (s.charAt(i) != ' ')
                    count++;
                if (s.charAt(i) == ' ' && count > 0)
                    return count;
            }
            return count;
        }
    }

Log in to reply
 

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