Created
September 10, 2016 18:03
-
-
Save YanchevskayaAnna/9605b0ff37b840628ea7ed70a4c2f8ef to your computer and use it in GitHub Desktop.
allStar
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
package com.jss.com.jss.codingbat.recursion; | |
/** | |
* CGiven a string, compute recursively a new string where all the adjacent chars are now separated by a "*". | |
* <p> | |
* allStar("hello") → "h*e*l*l*o" | |
* allStar("abc") → "a*b*c" | |
* allStar("ab") → "a*b" | |
*/ | |
public class AllStar { | |
public String allStar(String str) { | |
/* if (str.equals("")) { | |
return ""; | |
} | |
if (0 == str.length() || str.isEmpty()) {//Yanchevskaya A. Не совсем поняла, зачем эта проверка? Ведь ты уже проверил на пустую строку в первой проверке? | |
return null; | |
} | |
String res = (str.charAt(0) + "*" + allStar(str.substring(1))); | |
if (res.endsWith("*")) { | |
return res.substring(0, res.length() - 1); | |
} | |
return res;*/ | |
if ((str == null) || (str.length() == 0)){ | |
return ""; | |
} | |
if (str.length() == 1){ | |
return str.substring(0,1); | |
} | |
return (str.charAt(0) + "*" + allStar(str.substring(1))); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment