Skip to content

Instantly share code, notes, and snippets.

@sbsatter
Created August 19, 2016 14:00
Show Gist options
  • Save sbsatter/03782039c1f154ed0a76bdfdcee88624 to your computer and use it in GitHub Desktop.
Save sbsatter/03782039c1f154ed0a76bdfdcee88624 to your computer and use it in GitHub Desktop.
CodingBat: Recursion-1 > pairStar prev | next | chance 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"
class PairStar{
public static void main(String ... args){
System.out.println(pairStar(args[0]));
}
public static String pairStar(String str) {
System.out.println(str);
if(str.length()<=1){
return str;
}
int mid= str.length()/2;
String str1=pairStar(str.substring(0,mid));
String str2=pairStar(str.substring(mid,str.length()));
// System.out.println();
char c;
if((c=str1.charAt(str1.length()-1))==str2.charAt(0)){
return str1+"*"+str2;
}else{
return str1+str2;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment