easy to understand java solution


  • 0
    Z

    class Solution {
    public int lengthOfLastWord(String s) {

        int n = s.length();
        if (n==0) return 0;
        int count = 0;
        while(s.charAt(n-1) == ' ')
             { 
               n--; 
               if(n == 0) return 0;
             
             }
        
        while(s.charAt(n-1) != ' ' ){
            
            count++;
            n--;
            if (n == 0) break;
            
        }
        return count;
        
    }
    

    }


Log in to reply
 

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