knakul853

Untitled

Jul 22nd, 2020
175
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.59 KB | None | 0 0
  1. class Solution {
  2. public:
  3.     string countAndSay(int n) {
  4.        
  5.         if( n<1) return "";
  6.        
  7.         string ans = "1";
  8.         while(--n){
  9.            
  10.             string cur="";
  11.            
  12.             for(int i=0;i < (int)ans.size(); i++ ){
  13.                 int cnt=1;
  14.                 while(i+1<ans.size() && ans[i] == ans[i+1]){
  15.                     cnt++;
  16.                     i++;
  17.                 }
  18.                
  19.                 cur += to_string(cnt) + ans[i];
  20.             }
  21.            
  22.             ans = cur;
  23.         }
  24.        
  25.          return ans;
  26.     }
  27. };
Add Comment
Please, Sign In to add comment