-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSolution.java
More file actions
23 lines (23 loc) · 771 Bytes
/
Solution.java
File metadata and controls
23 lines (23 loc) · 771 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
class Solution {
public String countAndSay(int n) {
String res = "1";
for (int i = 1; i < n; i++) {
String stack = "";
String temp = "";
for (int j = 0; j < res.length(); j++) {
String c = res.substring(j, j + 1);
if (stack.length() > 0 && !stack.substring(stack.length() - 1).equals(c)) {
temp += stack.length() + stack.substring(stack.length() - 1);
stack = c;
} else {
stack += c;
}
}
if (stack.length() > 0) {
temp += stack.length() + stack.substring(stack.length() - 1);
}
res = temp;
}
return res;
}
}