-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcount_and_say.cpp
More file actions
36 lines (31 loc) · 822 Bytes
/
Copy pathcount_and_say.cpp
File metadata and controls
36 lines (31 loc) · 822 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
class Solution {
public:
string rec_countsay(int n, string s) {
if (n == 0) {
return s;
}
int curr_count = 0;
char curr_n = s[0];
string ans = "";
for (int i = 0; i < s.length(); ++i) {
++curr_count;
if (curr_n != s[i]) {
ans += to_string(curr_count - 1);
ans += curr_n;
curr_count = 1;
curr_n = s[i];
}
}
ans += to_string(curr_count);
ans += curr_n;
return rec_countsay(n - 1, ans);
}
string countAndSay(int n) {
if (n == 0) {
return "";
} else {
string s = "1";
return rec_countsay(n - 1, s);
}
}
};