Java - Simple and easy to understand


  • 0
    T
    public class Solution {
        public int lengthOfLastWord(String s) {
            int n = s.length();
            if(n==0) return 0;
            int i = n-1;
            
            while(i>=0){
                char c = s.charAt(i);
                if(Character.isLetter(c)){
                    int length = 0;
                    while(i>=0 && Character.isLetter(s.charAt(i))){
                        length++;
                        i--;
                    }
                    return length;
                }
                i--;
            }
            return 0;
        }
    }
    

Log in to reply
 

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