Solved

# removing x challenge

Posted on 2014-11-09
114 Views
Hi,

I am trying below example
http://codingbat.com/prob/p171260

I wrote my code as below
``````public String stringX(String str) {
StringBuilder sb=new StringBuilder("str");
for(int i=0;i<str.length()-1;i++)
{
if(str.substring(i,i+1).equals("x"))
sb.delete(i,i+1);
return sb.toString();
}
return sb.toString();
}
``````

0
Question by:gudii9
[X]
###### Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

• Help others & share knowledge
• Earn cash & points
• 13
• 7
• 5

LVL 84

Accepted Solution

ozo earned 250 total points
ID: 40431914
You might want sb to be built from the str that is passed in, rather than always using the same "str"
You may also want to make the code work when more than one character should be deleted.
0

LVL 27

Assisted Solution

dpearson earned 250 total points
ID: 40432030
If you're doing a series of deletes (like this function requires), it's hard to use the StringBuilder.delete() method because the character positions in that string are going to be different from the character positions in the string you are scanning.

str = "xabcx"
sb  = new StringBuilder("xabcx") ;

Now if you delete the first character from sb (sb.delete(0)) it changes the position of the remaining characters in the sb string as now it is "abcx".

So all in all, I think it's easier to just add strings to the buffer as you go.

Something like this:

``````public String stringX(String str) {
StringBuilder sb=new StringBuilder();
for(int i=0;i<str.length();i++)
{
String letter = str.substring(i,i+1) ;
if(i == 0 || i == str.length()-1 || !letter.equals("x"))
sb.append(letter) ;
}
return sb.toString();
}
``````

Do you see how this code is similar to what you wrote, but only does appends to the StringBuilder at the right time?

Doug
0

LVL 7

Author Comment

ID: 40433114
``````  String letter = str.substring(i,i+1) ;
if(i == 0 || i == str.length()-1 || !letter.equals("x"))
sb.append(letter) ;
``````

I am not very clear on above two lines. if it first or last or not x then apend to stringbuider sb(is buffer same as string builder..i see you used word buffer in your comment)

what happens if other than below case
if it first or last or not x then apend to stringbuider sb
I see all tests do pass.
0

LVL 7

Author Comment

ID: 40435933
You might want sb to be built from the str that is passed in, rather than always using the same "str"
You may also want to make the code work when more than one character should be deleted.

0

LVL 27

Expert Comment

ID: 40436562
(is buffer same as string builder..i see you used word buffer in your comment)

Yes StringBuffer and StringBuilder are (essentially) identical.  You should generally use StringBuilder.  It's a newer implementation and slightly better than StringBuffer, but both would work.

what happens if other than below case
if it first or last or not x then apend to stringbuider sb

We're looking to build up a new string that contains:
1) The first character
2) The last character
3) Any character that's not an x (between the first and last characters)

So this test
if(i == 0 || i == str.length()-1 || !letter.equals("x"))

does all 3 of those tests in one go.

If any of them are true, we add the character into the string we'll return.
Otherwise, we don't do anything and we just move on to the next character.

Does that make sense?

Doug
0

LVL 7

Author Comment

ID: 40437558
We're looking to build up a new string that contains:
1)  The first character
2) The last character

We're looking to build up a new string that contains:
1) Not The first character
2) Not The last character
0

LVL 7

Author Comment

ID: 40437839
3) Any character that's not an x (between the first and last characters)

i was clear on above point. But not below points
We're looking to build up a new string that contains:
1)  The first character
2) The last character
0

LVL 84

Expert Comment

ID: 40438912
an "x" at the very start or end should not be removed.
So an "x" at the very start or end should be included.
Also, a non "x" at the very start or end should be included.
So any character at the very start or end should be included.
0

LVL 7

Author Comment

ID: 40444060
`````` if(i == 0 || i == str.length()-1 || !letter.equals("x"))
sb.append(letter)
``````

can we break to few more lines and simplify above code to exactly see where is beolw things happening clearly

So an "x" at the very start or end should be included.
Also, a non "x" at the very start or end should be included.
So any character at the very start or end should be included.
0

LVL 84

Expert Comment

ID: 40444182
String letter = str.substring(i,i+1) ;
if(i == 0 || i == str.length()-1 || !letter.equals("x"))
sb.append(letter)
i == 0 â†’ letter at the very start
i == str.length()-1 â†’ letter at the very end
!letter.equals("x") â†’ letter a non "x"
These cover all the cases when character should be included
0

LVL 27

Expert Comment

ID: 40445983
can we break to few more lines and simplify above code to exactly see where is beolw things happening clearly
Sure - you can always break up an if statement into separate lines if you prefer.

So this:

``````if(i == 0 || i == str.length()-1 || !letter.equals("x"))
sb.append(letter)
``````

becomes this:

``````if (i == 0)
// We're at the start
sb.append(letter) ;
else if (i == str.length()-1)
// We're at the end
sb.append(letter) ;
else if (!letter.equals("x"))
// It's not an 'x'
sb.append(letter) ;
``````

It's more code, but it does the same thing.
0

LVL 7

Author Comment

ID: 40446173
If first and last even though x include.
Where we are giving above instruction
0

LVL 27

Expert Comment

ID: 40446356
If first and last even though x include.
Where we are giving above instruction

>> Except an "x" at the very start or end should not be removed.
0

LVL 7

Author Comment

ID: 40447211
If first and last even though x include.
Where we are giving above instruction

i mean in the code where we are taking care of it
0

LVL 7

Author Comment

ID: 40447220
else if (!letter.equals("x"))
// It's not an 'x'
sb.append(letter) ;

in the above code we are saying if no x(in any positions other than first and last) then add to string builder right?

``````if(i == 0 || i == str.length()-1 || !letter.equals("x"))
sb.append(letter)
``````

do not we should have like below instead of above
if(i == 0 && i == str.length()-1 && !letter.equals("x"))
sb.append(letter)
0

LVL 7

Author Comment

ID: 40447228
else if (!letter.equals("x"))
// It's not an 'x'
sb.append(letter) ;

so whatever comes inside above else if is the i when we are not at first and last index right
0

LVL 84

Expert Comment

ID: 40448962
i == 0 && i == str.length()-1
can only be true when str.length() is 1
0

LVL 84

Expert Comment

ID: 40448971
so whatever comes inside above else if is the i when we are not at first and last index right
given
if (i == 0)
// We're at the start
sb.append(letter) ;
else if (i == str.length()-1)
// We're at the end
sb.append(letter) ;
else if (!letter.equals("x"))
// It's not an 'x'
sb.append(letter) ;

the cases of the i when we are at first and last index are handled in the first two else statements, so if we get to the last else,  we are not at first and last index
0

LVL 7

Author Comment

ID: 40450906
i == 0 && i == str.length()-1
can only be true when str.length() is 1
our solution we are not using && but using || as below
I wonder what is relevance of && for this solution.
if(i == 0 || i == str.length()-1 || !letter.equals("x"))
0

LVL 27

Expert Comment

ID: 40451074
For this problem you want an "or" because we add the current character to the result if any of the conditions are true.
You want "and" when all of the conditions need to be true for the character to be added.

Doug
0

LVL 7

Author Comment

ID: 40453144
``````if (i == 0)
// We're at the start
sb.append(letter) ;
else if (i == str.length()-1)
// We're at the end
sb.append(letter) ;
else if (!letter.equals("x"))
// It's not an 'x'
sb.append(letter) ;
``````
Above if -else if-else if construct resembles to me like 'and' not 'or'. Please advise with some simple example
0

LVL 84

Expert Comment

ID: 40453728
The above if -else if-else if construct does sb.append(letter)  when any of the conditions are true.
The || construct also does sb.append(letter)  when any of the conditions are true.

i == 0 || i == str.length()-1 is true when either i == 0 is true or i == str.length()-1 is true.
i == 0 && i == str.length()-1 is only true when both i == 0 is true and i == str.length()-1 is true.
(which can only happen when str.length()==1)
0

LVL 7

Author Comment

ID: 40455469
The above if -else if-else if construct does sb.append(letter)  when any of the conditions are true.
The || construct also does sb.append(letter)  when any of the conditions are true.
now i know how to visualize those if -else if-else constructs, basically inside out, as inside common sb.append(letter)  appends when any one of conditions are met(not all)
0

LVL 7

Author Comment

ID: 40455475
similar to how we break
if(i == 0 || i == str.length()-1 || !letter.equals("x")

as simple steps of  if -else if-else if construct as below
``````if (i == 0)
// We're at the start
sb.append(letter) ;
else if (i == str.length()-1)
// We're at the end
sb.append(letter) ;
else if (!letter.equals("x"))
// It's not an 'x'
sb.append(letter) ;
``````

how to break into simple steps?
if(i == 0 && i == str.length()-1 && !letter.equals("x")
0

LVL 84

Expert Comment

ID: 40456369
if( i == 0 ){
if( i == str.length()-1 ){
if( !letter.equals("x") ){
sb.append(letter) ;
}
}
}
0

## Featured Post

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article will show, step by step, how to integrate R code into a R Sweave document
Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to aâ€¦
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relatâ€¦
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Botâ€¦
###### Suggested Courses
Course of the Month4 days, left to enroll