LeetCode/Algorithms

58. Length of Last Word

챠오위 2021. 10. 28. 23:58

 

Input 된 글자의 끝으로 간다.

for문을 돌려서 띄어쓰기가 없는 곳으로 간다.

띄어쓰기가 있을 때까지 count++ 

 

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;
            }
            
        }
        return count;
    }
}