Neat Java solution - No libraries used


  • 1
    M
    public class Solution {
        public int lengthOfLastWord(String s) {
            
            char[] chars = s.toCharArray();
            int i=chars.length-1;
            int count = 0;
            while (i>=0){
                while(i >=0 && chars[i]!=' '){
                        count += 1;
                        i -= 1;
                }
                if (count > 0)return count;
                i -= 1;
            }
            return 0;
            
        }
    }

Log in to reply
 

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