58. Length of Last Word

 class Solution {

public:

int lengthOfLastWord(string s) {


    int i = s.size( )-1;

    int idx=i;


// find the position of the first non-space character from the

// last of the string...

while(s[i]==' '){

i--;

}


    idx =i;

    

    int len =0;

    

    for(int k=idx;k>=0;k--){

        if(s[k]!=' ')

            len++;

        

        else

            break;

    }

    

    return len;

       

}


};

Comments

Popular posts from this blog

2161. Partition Array According to Given Pivot

2000. Reverse Prefix of Word

1769. Minimum Number of Operations to Move All Balls to Each Box