jayati

Length of Last Word

Feb 26th, 2023
166
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.42 KB | None | 0 0
  1. class Solution {
  2. public:
  3.     int lengthOfLastWord(string s) {
  4.         stack<char>st;
  5.         int count=0;
  6.         for(int i=0;i<s.length();i++)
  7.         {
  8.             st.push(s[i]);
  9.         }
  10.         while(st.top()==' ')
  11.         {
  12.             st.pop();
  13.         }
  14.         while(!st.empty() && st.top()!=' ')
  15.         {
  16.             count++;
  17.             st.pop();
  18.         }
  19.         return count;
  20.        
  21.     }
  22. };
Advertisement
Add Comment
Please, Sign In to add comment