-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCountAndSay.java
More file actions
31 lines (29 loc) · 864 Bytes
/
CountAndSay.java
File metadata and controls
31 lines (29 loc) · 864 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
// https://leetcode.com/problems/count-and-say/
public class CountAndSay {
public static void main(String[] args) {
new CountAndSay().countAndSay(30);
}
public String countAndSay(int n) {
String rle = "1";
for (int i = 2; i <= n; i++) {
rle = computeRLE(rle);
//System.out.println(i + " " + rle);
}
return new String(rle);
}
public static String computeRLE(String rle) {
int index = 0;
String update = "";
while (index < rle.length()) {
char c = rle.charAt(index);
int count = 1;
index++;
while (index < rle.length() && rle.charAt(index) == c) {
count++;
index++;
}
update = (update + count) + c;
}
return update;
}
}