-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathProblem-1.java
69 lines (52 loc) · 1.22 KB
/
Problem-1.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
/**
* @author AkashGoyal
*/
/**
--------------------- Problem----------->> Count and Say
Problem Link :- https://leetcode.com/problems/count-and-say/
*/
class Solution {
/*
Example:-1
n=4
1--> 1--> one 1
2--> 11--> two 1
3--> 21--> one 2 one 1
4--> 1211
*/
public String countAndSay(int n) {
String start="1";
int count=1;
if(n==1)
{
return "1";
}
while(n>1)
{
start=checkcount(start);
n--;
}
return start;
}
static String checkcount(String start)
{
int count=1;
StringBuilder sb=new StringBuilder();
for(int i=0;i<start.length()-1;i++)
{
if(start.charAt(i)!=start.charAt(i+1))
{
sb.append(count);
sb.append(start.charAt(i));
count=1;
}
else
{
count++;
}
}
sb.append(count);
sb.append(start.charAt(start.length()-1));
return sb.toString();
}
}