-
Notifications
You must be signed in to change notification settings - Fork 51
/
Question_14_notfound.java
50 lines (42 loc) · 1.19 KB
/
Question_14_notfound.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
/*
Question 14
___________
*/
package hactoberfest;
import java.util.Scanner;
public class CountandSAY {
public String Count(String s){
int cnt = 1;
char ch = s.charAt(0);
StringBuilder curr = new StringBuilder();
for(int i=1;i<s.length();i++){
if(s.charAt(i)==ch){
cnt++;
}
else{
curr.append(cnt);
curr.append(ch);
ch = s.charAt(i);
cnt = 1;
}
}
curr.append(cnt);
curr.append(ch);
return curr.toString();
}
public String countAndSay(int n) {
String s = "1";
for(int i=1;i<n;i++){
s = Count(s);
}
return s;
}
public static void main(String[] args) {
CountandSAY obj = new CountandSAY();
Scanner sc = new Scanner(System.in);
System.out.println("Enter number");
int n = sc.nextInt();
String ans = obj.countAndSay(4);
System.out.println(ans);
}
}