forked from avinashbest/java-coding-ninjas
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPairStar.java
More file actions
42 lines (37 loc) · 1.04 KB
/
PairStar.java
File metadata and controls
42 lines (37 loc) · 1.04 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
package recursion2.Assignment;
import java.util.Scanner;
/*Given a string S, compute recursively a new string where identical chars that are adjacent in the original string are separated from each other by a "*".
Input format :
String S
Output format :
Modified string
Constraints :
0 <= |S| <= 1000
where |S| represents length of string S.
Sample Input 1 :
hello
Sample Output 1:
hel*lo
Sample Input 2 :
aaaa
Sample Output 2 :
a*a*a*a*/
public class PairStar {
public static String pairStar(String str) {
if (str.length() <= 1) {
return str;
}
String smallAnswer = pairStar(str.substring(1));
if (str.charAt(0) == str.charAt(1)) {
smallAnswer = str.charAt(0) + "*" + str.charAt(1) + smallAnswer.substring(1);
} else {
smallAnswer = str.charAt(0) + smallAnswer;
}
return smallAnswer;
}
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
String str = scan.next();
System.out.println(pairStar(str));
}
}