Java > String-2 > plusOut (CodingBat Solution)

Problem:

Given a string and a non-empty word string, return a version of the original String where all chars have been replaced by pluses ("+"), except for appearances of the word string which are preserved unchanged.

plusOut("12xy34", "xy") → "++xy++"
plusOut("12xy34", "1") → "1+++++"
plusOut("12xy34xyabcxy", "xy") → "++xy++xy+++xy"


Solution:

public String plusOut(String str, String word) {
  int slen = str.length();
  int wlen = word.length();
  String fin = "";
  
  for (int i = 0; i < slen; i++) {
    if (i <= slen - wlen) {
      String tmp = str.substring(i,i+wlen);
      if (tmp.equals(word)) {
        fin += word;
        i += wlen-1;
      }
      else
        fin += "+";
    }
    else
      fin += "+";
  }
  
  return fin;
}


23 comments :

  1. can you please comment about the efficiency of this code:(good or bad)

    public String plusOut(String str, String word) {
    if(str.contains(word)){
    str=str.replace(word,"--");
    for(int i=0;i<str.length();i++){
    if(str.charAt(i)!='-')
    str=str.replace(str.substring(i,i+1),"+");
    }
    }
    str=str.replace("--",word);
    return str;
    }

    ReplyDelete
    Replies
    1. I like your solution, it is very clear.

      Delete
    2. Nice solution, yea, BUT! If String str contains some "-" at first place? It will make wrong results!

      Delete
  2. can someone comment on this

    public String plusOut(String str, String word) {
    StringBuilder result = new StringBuilder ("");
    int count = 0;
    int jump = word.length();
    for (int i = 0; i <str.length()-jump+1;i++){

    if (str.substring(i,i+word.length()).equals(word) && count <1) {
    result.append(word);
    i = i+jump-1;
    }
    else result.append("+");
    }
    if (!word.substring(jump-1).equals(str.substring(str.length()-1))) {
    for (int i2 = 0; i2<jump-1;i2++){
    result.append("+");
    }
    }
    return result.toString();
    }

    ReplyDelete
  3. Can anyone comment on this? I wanted to avoid making another variable.

    My idea was that I only had to check for a word in the first place if str.charAt(currentIndex) was equal to the first letter of the word.

    public String plusOut(String str, String word) {
    for (int i = 0; i < str.length(); ++i) {
    if (str.charAt(i) == word.charAt(0)) {
    if ((str.substring(i, i + word.length()).equals(word))) {
    i += word.length() -1; // ++i runs right after this.
    } else str = str.substring(0, i) + "+" + str.substring(i + 1, str.length());
    }
    else str = str.substring(0, i) + "+" + str.substring(i + 1, str.length());
    }
    return str;
    }

    ReplyDelete
  4. public String plusOut(String str, String word) {

    for(int i=str.lastIndexOf(word)+word.length();i<str.length();i++) {
    str=str.replace(str.substring(i,i+1), "+");
    }
    int start=0;
    while (start < str.length()) {
    //System.out.println(start);
    if (str.indexOf(word,start) == -1)
    return str;
    int id = str.indexOf(word, start);
    String sub = str.substring(start,id), plus="";
    //System.out.println(sub);
    for(int j=0; j<sub.length(); j++) { plus+="+"; }
    //System.out.println("sub " + sub + " TBR with " + plus);
    str=str.replace(sub,plus);
    start= id+word.length();
    //System.out.println(start);
    }
    return str;
    }

    ReplyDelete
  5. public String plusOut(String str, String word) {
    if (str.length() == 0) return "";
    return str.length() >= word.length() && str.substring(0, word.length()).equals(word) ? word + plusOut(str.substring(word.length(), str.length()), word):"+"+plusOut(str.substring(1, str.length()), word);
    }

    ReplyDelete
  6. This is a nice solution:
    public String plusOut(String str, String word) {
    String plus = "", newStr = "";

    while(str.length()>=word.length()){
    if(!str.substring(0,word.length()).equals(word)){
    newStr+= "+";
    str = str.substring(1);
    }
    else{
    newStr += word;
    str = str.substring(word.length());
    }
    }

    for(int i = 0; i < str.length(); i++)
    newStr+= "+";


    return newStr;
    }

    ReplyDelete
  7. My solution is similar to the one at the top of the page (and not as elegant, I must admit). What I found tricky about this problem is that the variable, i, will increment by 1 if we are merely replacing a given character with a "+", but it will increment by the length of the target word if we encounter the target. So, I created a variable, pos, to keep track of the position of i.

    public String plusOut(String str, String word) {
    String result = "";

    int sl public String plusOut(String str, String word) {
    String result = "";

    int sl = str.length();
    int wl = word.length();

    int pos = 0;

    for(int i = pos; i <= sl - wl; i++){
    if(str.substring(i, i + wl).equals(word)){
    result += word;
    i += wl - 1;
    pos += word.length();
    } else {
    result += "+";
    pos++;
    }
    }

    for(int i = pos; i < str.length(); i++){
    result += "+";
    }

    return result;

    }= str.length();
    int wl = word.length();

    int pos = 0;

    for(int i = pos; i <= sl - wl; i++){
    if(str.substring(i, i + wl).equals(word)){
    result += word;
    i += wl - 1;
    pos += word.length();
    } else {
    result += "+";
    pos++;
    }
    }

    for(int i = pos; i < str.length(); i++){
    result += "+";
    }

    return result;

    }

    ReplyDelete
    Replies
    1. Wow...that got screwed up. Correct code is:

      public String plusOut(String str, String word) {

      String result = "";

      int sl = str.length();
      int wl = word.length();

      int pos = 0;

      for(int i = pos; i <= sl - wl; i++){
      if(str.substring(i, i + wl).equals(word)){
      result += word;
      i += wl - 1;
      pos += word.length();
      } else {
      result += "+";
      pos++;
      }
      }

      for(int i = pos; i < str.length(); i++){
      result += "+";
      }

      return result;

      }

      Delete
  8. public String plusOut(String str, String word) { //13 && 2
    String result = "";
    for (int i=0 ; i<=str.length()-word.length(); i++) {
    if (!str.substring(i,i+word.length()).equals(word)) {
    result = result + "+";
    }
    else {
    result = result + word;
    i=i+word.length()-1;
    }
    }
    for (int a = 1; a<word.length(); a++) {
    if (!str.endsWith(word)) {
    result = result + "+";
    }
    }
    return result;
    }

    ReplyDelete
  9. public String plusOut(String str, String word) {
    String result="";
    for(int i=0;i<str.length();i++)
    {
    if(i<=str.length()-word.length()){
    if(word.equals(str.substring(i,i+word.length()))){result+=word;i+=word.length()-1;}else
    result+="+";}
    else
    result+="+";
    }return result;
    }

    ReplyDelete
  10. public String plusOut(String str, String word) {
    String result = "";
    String temp = "";
    int j = 0;
    for(int i = 0; i < str.length(); i++){
    if(str.charAt(i) != word.charAt(j)){
    result = result + "+";
    if(temp.length()!=0){
    for(int k = 0; k < temp.length(); k++){
    result = result + "+";
    }
    }
    temp = "";
    j = 0;
    } else {
    temp = temp + str.charAt(i);
    if(word.length() == temp.length()){
    result = result + temp;
    j = 0;
    temp = "";
    } else {
    j++;
    }
    }
    }
    return result;
    }

    ReplyDelete
  11. public String plusOut(String str, String word) {
    String output = "";
    int sl = str.length();
    int wl = word.length();

    for (int i = 0; i < sl; i++) {
    if ((i <= sl - wl) && (str.substring(i , i + wl).equals(word))) {
    output += word;
    i += wl - 1;
    continue;
    }
    output += "+";
    }
    return output;
    }

    ReplyDelete
  12. With the Bowflex TreadClimber, you can change the height and the intensity quite easily. treadclimber

    ReplyDelete
  13. public String plusOut(String str, String word) {
    String change = "";
    for (int i = 0; i < str.length(); i++)
    {
    if (i + word.length() <= str.length() && str.substring(i, i + word.length()).equals(word))
    {
    change += word;
    i += word.length() - 1;
    }
    else
    {
    change += '+';
    }
    }
    return change;
    }

    ReplyDelete
  14. Essentially, the Bowflex TreadClimber allows you to use a combination of moves to get the right kind of effect. treadclimber reviews

    ReplyDelete
  15. public String plusOut(String str, String word) {
    int i=0;
    String ret="";
    while(i<str.length()){
    if(str.substring(i).startsWith(word)){
    ret += word;
    i += word.length();
    }else{
    ret += "+";
    i++;
    }
    }
    return ret;
    }

    ReplyDelete
  16. public String plusOut(String str, String word) {

    if(str.contains(word)){
    str=str.replace(word,"--");//بدل كل الحروف المطلوب البحث عنتها هنا بسالب مثلا او أي رمز
    for(int i=0;i<str.length();i++){
    if(str.charAt(i)!='-')//لو ملقتهوش
    str=str.replace(str.substring(i,i+1),"+");//حط اللي بتختبره بالرمز المطلوب
    }
    }
    str=str.replace("--",word);//بدل كل الرموز السالب بالوورد تاني
    return str;
    }




    /*str=str.replaceAll(word,"00");
    for(int i=0 ; i<str.length() ; i++){
    if(str.charAt(i)!='0' ) str=str.replace(str.substring(i,i+1),"+");

    }

    str=str.replaceAll("0",word);
    return str;

    }*/

    ReplyDelete
  17. public static String plusOut(String str, String word) {
    int lenWord = word.length();
    String result = "";

    for(int i = 0; i < str.length()-lenWord; i++){
    if(!str.substring(i,i+lenWord).equals(word)){
    result += "+";
    } else{
    result += str.substring(i,i+lenWord);
    i+=lenWord-1;

    }
    }
    int dif = str.length() - result.length();
    if(str.endsWith(word)){
    result += word;
    } else {
    for(int i = 0; i< dif; i++){
    result+="+";
    }
    }
    return result;
    }

    ReplyDelete
  18. public static void plusout(String a,String b) {

    String ans = "";

    for(int i=0;i<a.length();i++) {

    if(!b.contains(a.charAt(i)+"")) {

    ans = ans + "+";
    }else {

    ans = ans + a.charAt(i);
    }
    }

    System.out.println(ans);
    }

    ReplyDelete
  19. public String plusOut(String str, String word) {
    String result = "";
    int position = 0;

    for (int i = 0; i < str.length(); i++) {
    if (i == str.indexOf(word, position)){
    result = result + word;
    position = i + word.length();
    i += word.length()-1;
    }
    else {
    result = result + "+";
    }
    }
    return result;
    }

    ReplyDelete
  20. public String plusOut(String str, String word) {
    String s = str.replace(word, "*");
    s = s.replaceAll("([^\\*])", "+");
    return s.replace("*", word);
    }

    ReplyDelete

Follow Me

If you like our content, feel free to follow me to stay updated.

Subscribe

Enter your email address:

We hate spam as much as you do.

Upload Material

Got an exam, project, tutorial video, exercise, solutions, unsolved problem, question, solution manual? We are open to any coding material. Why not upload?

Upload

Copyright © 2012 - 2014 Java Problems  --  About  --  Attribution  --  Privacy Policy  --  Terms of Use  --  Contact