-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathCountAndSay.java
94 lines (85 loc) · 2.24 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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
/*https://leetcode.com/problems/count-and-say/*/
/*Prateekshya's Solution*/
class Solution {
public String countAndSay(int n) {
if (n == 1) return "1";
StringBuilder str = new StringBuilder("1");
while (n > 1)
{
StringBuilder say = new StringBuilder("");
int count = 0;
char curr = '0';
for (int i = 0; i < str.length(); ++i)
{
//if current character is not same as the previous
if (curr != str.charAt(i))
{
//update the say
if (curr != '0')
{
say.append(count);
say.append(curr);
}
//update the curr and count
curr = str.charAt(i);
count = 1;
}
//if same then increment count
else
{
++count;
}
}
//update the say
if (curr != '0')
{
say.append(count);
say.append(curr);
}
//store it as str for next iteration
str = new StringBuilder(say);
//decrease n
--n;
}
return new String(str);
}
}
/*Pratik's Solution*/
class Solution {
public String countAndSay(int n)
{
if(n==1)return "1";
if(n==2)return "11";
return count(countAndSay(n-1));
}
public String count(String s)
{
int count = 1;
String res = new String();
for(int i=0;i<s.length()-1;i++)
{
if(s.charAt(i)==s.charAt(i+1))
{
count++;
}
else
{
String c = String.valueOf(count);
res+=c;
res+=s.charAt(i);
count = 1;
}
}
if(s.charAt(s.length()-1)==s.charAt(s.length()-2))
{
res+=String.valueOf(count);
res+=s.charAt(s.length()-1);
}
else
{
res+='1';
res+=s.charAt(s.length()-1);
}
return res;
}
}