Advertisement
vaibhav1906

Sol for Mihir

Sep 4th, 2022
964
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.74 KB | None | 0 0
  1. class Solution {
  2. public:
  3.     string countAndSay(int n) {
  4.        
  5.         string s = "1";
  6.         char c;
  7.         int count;
  8.         while(n>1){
  9.             string temp = "";
  10.             count = 1;
  11.             c = s[0];
  12.            
  13.             for(int i = 1; i< s.length(); i++){
  14.                 if(s[i]==c){
  15.                     count++;
  16.                 }
  17.                 else{
  18.                     temp += to_string(count);
  19.                     temp += c;
  20.                     count = 1;
  21.                     c = s[i];
  22.                 }
  23.             }
  24.             temp += to_string(count);
  25.             temp += c;
  26.             s = temp;
  27.             n--;
  28.         }
  29.        
  30.        
  31.        
  32.         return s;
  33.        
  34.     }
  35. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement