Answers for "58. Length of Last Word leetcode solution in c++"

C++
0

58. Length of Last Word leetcode solution in c++

class Solution {
public:
    int lengthOfLastWord(string s) {
        int n=s.length();
        bool flag=false;
        int cnt=0;
        for(int i=n-1;i>=0;i--)
        {
            if(s[i]>=65&&s[i]<=90||s[i]>=97&&s[i]<=122)
            {
                cnt++;
                flag=true;
            }else
            {
                if(flag)
                {
                    break;
                }
            }
        }
        return cnt;
    }
};
Posted by: Guest on April-04-2022

Browse Popular Code Answers by Language