Easy to Understand Java Solution- 5ms


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

Log in to reply
 

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