A simple java solution


  • 0
    M

    class Solution {
    public int lengthOfLastWord(String s) {
    int sum=0;
    for(int i=s.length()-1;i>=0;i--)
    {
    if(Character.isLetterOrDigit(s.charAt(i)))
    {
    sum++;
    }

                if (Character.isWhitespace(s.charAt(i)) && sum!=0)
                break;
            
        }
        return sum;
    }
    

    }


Log in to reply
 

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