4

This is a problem from the CodingBat website. I am pasting the problem first and discussing my efforts after that:

Given two strings, base and remove, return a version of the base string where all instances of the remove string have been removed (not case sensitive). You may assume that the remove string is length 1 or more. Remove only non-overlapping instances, so with "xxx" removing "xx" leaves "x".

withoutString("Hello there", "llo") → "He there"
withoutString("Hello there", "e") → "Hllo thr"
withoutString("Hello there", "x") → "Hello there"

This is what I wrote so far:

public String withoutString(String base, String remove) {

   int len_b=base.length();
   int len_r = remove.length();
   String result="";

   if(len_b<1 || len_r<1)
   return "";

   for (int i =0;i<=len_b-len_r;i++)
   {
      if(base.substring(i,i+len_r).equals(remove))
      {
        i=i+len_r-1;
      }

      else
      { 
        result=result+base.substring(i,i+1);
      }  
   } 

   if(!(base.substring(len_b-len_r+1, len_b).equals(remove)))
   result=result+base.substring(len_b-len_r+1, len_b);

return result;
}

This passes all the test cases except for the ones where the removal of the string should be case-insensitive.

For example: withoutString("This is a FISH", "IS") → "Th a FH"

My code gives me "This is a FH" as I haven't handled case sensitivity in my code. I know that with Regex this could be done in one line. I am more interested in knowing if there is a way to handle these kinds of test cases in my present code. Also, please let me know if my code could be made more efficient/elegant.

0

5 Answers 5

6

String has an equalsIgnoreCase(String s) method.

Sign up to request clarification or add additional context in comments.

1 Comment

Worked like a charm! Thanks! Now it passes all the tests, but somehow fails "other tests" on the codingbat website. Not sure why.
1

you can change this statement base.substring(i,i+len_r).equals(remove) to base.substring(i,i+len_r).equalsIgnoreCase(remove) using equalsIgnoreCase method.

hope helpful.

Comments

1

I did it without any looping :) I suppose it is not the best answer, but it works though

public String withoutString(String base, String remove) {
    String lastString = base.replace(remove, "");
    remove = remove.toLowerCase();
    String veryLastString = lastString.replace(remove, "");
    remove = remove.toUpperCase();
    String veryVeryLastString = veryLastString.replace(remove, "");
    return veryVeryLastString;
}

Comments

0
public String withoutString(String base, String remove) 
{
    String str=base;
    String str1=remove;
    String str3=str;

    int k=str1.length();

    for(int i=0;i<(str.length()-k+1);i++)
    {
        if(str1.equalsIgnoreCase(str.substring(i, i+k)))
        {
            String str4=str.substring(i, i+k);
            str3=str3.replaceFirst(str4,"" );

        }
    }
    return str3;
}

Comments

0
public String withoutString(String base, String remove) {
      String b=base.toLowerCase();
      String r=remove.toLowerCase();
      if(b.length()<r.length()) return base;
      if(b.contains(r)) b=b.replaceAll(r,"");
      String temp="";
      int j=0;
      for(int i=0;i<base.length();i++)
        if(j<b.length()){
          if(base.substring(i,i+1).equalsIgnoreCase(b.substring(j,j+1))){
            temp+=base.substring(i,i+1);
            j++;
          }
        }  
      return temp;
    }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.