forked from architsingla13/InterviewBit-Solutions
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCountAndSay.java
More file actions
52 lines (42 loc) · 1.1 KB
/
CountAndSay.java
File metadata and controls
52 lines (42 loc) · 1.1 KB
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
package String;
/**
* Author - archit.s
* Date - 05/10/18
* Time - 12:51 AM
*/
public class CountAndSay {
public String countAndSay(int A) {
if(A == 1){
return "1";
}
StringBuilder s = new StringBuilder("11");
int count = 0;
if(A == 2){
return s.toString();
}
while(A != 2){
StringBuilder temp = new StringBuilder();
char value = s.charAt(0);
count = 1;
for(int i=1;i<s.length();i++){
if(s.charAt(i) == value){
count++;
}
else{
temp.append(String.valueOf(count));
temp.append(value);
value = s.charAt(i);
count = 1;
}
}
temp.append(String.valueOf(count));
temp.append(value);
A--;
s = temp;
}
return s.toString();
}
public static void main(String[] args) {
System.out.println(new CountAndSay().countAndSay(4));
}
}