Skip to content

Commit 1dfd09d

Browse files
authored
n^2 potentially; Iteratively reading prev str
1 parent 0ec4081 commit 1dfd09d

File tree

1 file changed

+25
-0
lines changed

1 file changed

+25
-0
lines changed

Count and Say.java

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
class Solution {
2+
public String countAndSay(int n) {
3+
String prev = "1";
4+
for(int i = 1; i < n; i++) {
5+
StringBuilder str = new StringBuilder();
6+
int count = 0;
7+
char prevC = ' ';
8+
for(int j = 0; j < prev.length(); j++) {
9+
char c = prev.charAt(j);
10+
if(j == 0 || c == prevC) {
11+
prevC = c;
12+
count++;
13+
}
14+
else {
15+
str.append(count).append(prevC);
16+
count = 1;
17+
prevC = c;
18+
}
19+
}
20+
if(count != 0) str.append(count).append(prevC);
21+
prev = str.toString();
22+
}
23+
return prev;
24+
}
25+
}

0 commit comments

Comments
 (0)