Solved

removing x challenge

Posted on 2014-11-09
25
97 Views
Last Modified: 2014-11-20
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();
}

Open in new window


Please advise on how to fix and improve the my code. Thanks in advance
0
Comment
Question by:gudii9
  • 13
  • 7
  • 5
25 Comments
 
LVL 84

Accepted Solution

by:
ozo earned 250 total points
Comment Utility
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 26

Assisted Solution

by:dpearson
dpearson earned 250 total points
Comment Utility
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.

Say you start with
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();
}

Open in new window


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

by:gudii9
Comment Utility
  String letter = str.substring(i,i+1) ;
  if(i == 0 || i == str.length()-1 || !letter.equals("x"))
    sb.append(letter) ;

Open in new window


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.
Please advise
0
 
LVL 7

Author Comment

by:gudii9
Comment Utility
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.

can you please advise on this.
0
 
LVL 26

Expert Comment

by:dpearson
Comment Utility
(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

by:gudii9
Comment Utility
We're looking to build up a new string that contains:
1)  The first character
2) The last character

it should like below right? please advise

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

by:gudii9
Comment Utility
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
please advise
0
 
LVL 84

Expert Comment

by:ozo
Comment Utility
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

by:gudii9
Comment Utility
 if(i == 0 || i == str.length()-1 || !letter.equals("x"))
    sb.append(letter) 

Open in new window


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.
please advise
0
 
LVL 84

Expert Comment

by:ozo
Comment Utility
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 26

Expert Comment

by:dpearson
Comment Utility
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) 

Open in new window


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) ;

Open in new window


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

Author Comment

by:gudii9
Comment Utility
If first and last even though x include.
Where we are giving above instruction
0
Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 
LVL 26

Expert Comment

by:dpearson
Comment Utility
If first and last even though x include.
Where we are giving above instruction

The problem statement had this:
   >> Except an "x" at the very start or end should not be removed.
0
 
LVL 7

Author Comment

by:gudii9
Comment Utility
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

by:gudii9
Comment Utility
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) 

Open in new window



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

by:gudii9
Comment Utility
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

by:ozo
Comment Utility
i == 0 && i == str.length()-1
can only be true when str.length() is 1
0
 
LVL 84

Expert Comment

by:ozo
Comment Utility
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

by:gudii9
Comment Utility
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 26

Expert Comment

by:dpearson
Comment Utility
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

by:gudii9
Comment Utility
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) ;

Open in new window

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

by:ozo
Comment Utility
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

by:gudii9
Comment Utility
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

by:gudii9
Comment Utility
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) ;

Open in new window


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

Expert Comment

by:ozo
Comment Utility
if( i == 0 ){
     if( i == str.length()-1 ){
          if( !letter.equals("x") ){
               sb.append(letter) ;
          }
     }
}
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Updating Java 9 87
SQL400 max size 5 52
java set up 1 45
Problem to page 4 16
This article is meant to give a basic understanding of how to use R Sweave as a way to merge LaTeX and R code seamlessly into one presentable document.
This article will show, step by step, how to integrate R code into a R Sweave document
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.
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…

772 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now