My Java solution


  • 0
    Z

    '''
    public class Solution {
    public int lengthOfLastWord(String s) {

        int lengthS=s.length();
        int a=-1,b=-1,count=0;
        
        for(int i=lengthS-1;i>-1;i--){
         
            if(count==0&&s.charAt(i)!=' '){
                b=i;
                count=1;
            }
            if(count==1&&s.charAt(i)==' '){
                a=i;
                return b-a;
            }
            
        }
        
        return b-a;
    }
    

    }
    '''


Log in to reply
 

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