-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathCountAndSay.java
57 lines (46 loc) · 1.41 KB
/
CountAndSay.java
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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
public class Solution {
public String countAndSay(int n) {
if (n == 1) return "1";
if (n == 2) return "11";
String str = "11";
for (int i = 3; i <= n; i++)
{
// In below for loop, previous
// character is processed in
// current iteration. That is
// why a dummy character is
// added to make sure that loop
// runs one extra iteration.
str += '$';
int len = str.length();
int cnt = 1; // Initialize count
// of matching chars
String tmp = ""; // Initialize i'th
// term in series
char []arr = str.toCharArray();
// Process previous term
// to find the next term
for (int j = 1; j < len; j++)
{
// If current character
// does't match
if (arr[j] != arr[j - 1])
{
// Append count of
// str[j-1] to temp
tmp += cnt + 0;
// Append str[j-1]
tmp += arr[j - 1];
// Reset count
cnt = 1;
}
// If matches, then increment
// count of matching characters
else cnt++;
}
// Update str
str = tmp;
}
return str;
}
}