Skip to content

Instantly share code, notes, and snippets.

@YanchevskayaAnna
Created September 10, 2016 18:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save YanchevskayaAnna/9605b0ff37b840628ea7ed70a4c2f8ef to your computer and use it in GitHub Desktop.
Save YanchevskayaAnna/9605b0ff37b840628ea7ed70a4c2f8ef to your computer and use it in GitHub Desktop.
allStar
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