forked from dscmsit/Problem-Solving-in-any-Language
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCountAndSay.java
More file actions
30 lines (29 loc) · 705 Bytes
/
CountAndSay.java
File metadata and controls
30 lines (29 loc) · 705 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
class CountAndSay {
public String countIdx(String s){
StringBuilder sb = new StringBuilder();
char c = s.charAt(0);
int count = 1;
for(int i = 1; i < s.length(); i++){
if(s.charAt(i) == c){
count++;
}
else
{
sb.append(count);
sb.append(c);
c = s.charAt(i);
count = 1;
}
}
sb.append(count);
sb.append(c);
return sb.toString();
}
public String countAndSay(int n) {
String s = "1";
for(int i = 1; i < n; i++){
s = countIdx(s);
}
return s;
}
}