-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path38.count-and-say.cpp
More file actions
43 lines (29 loc) · 996 Bytes
/
38.count-and-say.cpp
File metadata and controls
43 lines (29 loc) · 996 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
class Solution {
public:
string countAndSay(int n) {
if(n == 1)
return "1";
if(n==2)
return "11";
string res = countAndSay(n-1);
// cout<<"\nResult recvd : "<<res << endl;
string temp = "";
int i = 0;
// string res = "21";
for(; i<res.size(); i++){
cout<<"i:"<<i<<endl;
int count = 1;
char c = res[i];
// cout<<"i:"<<i<<endl;
while(i+1<res.size() && res[i+1] == c){
count++;
// cout<<"Inside while: i :"<<i<<endl;
i++;
}
temp+=to_string(count)+c;
// cout<<i<<":i \tTemp : "<<temp<<endl;
// cout<<"Temp : "<<temp<<endl;
}
return temp;
}
};