-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCountAndSay.cpp
More file actions
35 lines (33 loc) · 810 Bytes
/
CountAndSay.cpp
File metadata and controls
35 lines (33 loc) · 810 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
#include <iostream>
#include <string>
#include <sstream>
using namespace std;
class Solution {
public:
int consecutive(string &str, int index){
char c = str[index];
for(int i=index+1; i<str.size(); i++)
if(str[i] != c)
return i-index;
return str.size()-index;
}
string countAndSay(int n) {
string s = "1";
for(int i=0; i<n-1; i++){
stringstream ss;
int index = 0;
while(index < s.size()){
int con = consecutive(s, index);
ss << con << s[index];
index += con;
}
s = ss.str();
}
return s;
}
};
int main(int argc, char *argv[]){
Solution sol;
cout << sol.countAndSay(4) << endl;
return 0;
}