Skip to content

Commit 4f7f0bd

Browse files
EASY/src/easy/CountandSay.java
1 parent c234e29 commit 4f7f0bd

File tree

1 file changed

+28
-0
lines changed

1 file changed

+28
-0
lines changed

EASY/src/easy/CountandSay.java

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package easy;
2+
3+
public class CountandSay {
4+
5+
public String countAndSay(int n) {
6+
StringBuilder curr = new StringBuilder("1");
7+
StringBuilder prev;
8+
int count;
9+
char say;
10+
for(int i = 1; i < n; i++){
11+
prev = curr;
12+
curr = new StringBuilder();
13+
count = 1;
14+
say = prev.charAt(0);
15+
16+
for(int j = 1, len = prev.length(); j < len; j++){
17+
if(prev.charAt(j) != say){
18+
curr.append(count).append(say);
19+
count = 1;
20+
say = prev.charAt(j);
21+
} else count++;
22+
}
23+
curr.append(count).append(say);
24+
}
25+
return curr.toString();
26+
}
27+
28+
}

0 commit comments

Comments
 (0)