Created
August 19, 2016 14:00
-
-
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"
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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