-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPairStar.java
More file actions
21 lines (15 loc) · 749 Bytes
/
Copy pathPairStar.java
File metadata and controls
21 lines (15 loc) · 749 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
//https://codingbat.com/prob/p158175
/*Given a string, compute recursively a new string where identical chars that are adjacent in the original string are separated from each other by a "*".
pairStar("hello") → "hel*lo"
pairStar("xxyy") → "x*xy*y"
pairStar("aaaa") → "a*a*a*a"*/
public String pairStar(String str) {
if(str.length()==0) return "";
if(str.length()==1) return str;
//if(str.substring(0,1).equals(str.substring(1,2)))
//return str.substring(0,1)+"*"+pairStar(str.substring(1));
//return str.substring(0,1)+pairStar(str.substring(1));
//OR
return (str.substring(0,1).equals(str.substring(1,2)))?
str.substring(0,1)+"*"+pairStar(str.substring(1)):str.substring(0,1)+pairStar(str.substring(1));
}