We help IT Professionals succeed at work.

getSandwich  java challenge

580 Views
Last Modified: 2016-03-29
Hi,
I am working on below challenge
http://codingbat.com/prob/p129952
I tried my code as below
public String getSandwich(String str) {
  String Str1 = new String("Welcome to Tutorialspoint.com");
  //String str2=str.replaceFirst("bread", "" );
  //System.out.println(Str.replaceFirst("Tutorials", "" ));
//String strNew= str.repalceFirst("bread", "xyzzz");
String str3=str.replaceAll(".*bread*.", "" );

  return str3;
}

 //System.out.println(Str.replaceFirst("(.*)Tutorials(.*)",
 //                        "AMROOD" ));

 //     System.out.print("Return Value :" );
 //     System.out.println(Str.replaceFirst("Tutorials", "AMROOD" ));

Open in new window

I am getting below result
Expected      Run            
getSandwich("breadjambread") → "jam"      ""      X         
getSandwich("xxbreadjambreadyy") → "jam"      "y"      X         
getSandwich("xxbreadyy") → ""      "y"      X         
getSandwich("xxbreadbreadjambreadyy") → "breadjam"      "y"      X         
getSandwich("breadAbread") → "A"      ""      X         
getSandwich("breadbread") → ""      ""      OK         
getSandwich("abcbreaz") → ""      ""      OK         
getSandwich("xyz") → ""      "xyz"      X         
getSandwich("") → ""      ""      OK         
getSandwich("breadbreaxbread") → "breax"      ""      X         
getSandwich("breaxbreadybread") → "y"      ""      X         
getSandwich("breadbreadbreadbread") → "breadbread"      ""      X         
other tests
X      

how to  improve my approach, results and design of this challenge. How do i make a graphical venn or flow chart or some other relevant diagram to design it before writing code to decide best strategy?
 Please advise
Comment
Watch Question

ozo
CERTIFIED EXPERT
Most Valuable Expert 2014
Top Expert 2015

Commented:
public String getSandwich(String str) {
  return str.replaceFirst("(?:(?!bread).)*(?:bread(.*)bread)?.*","$1");
}

Author

Commented:
public String getSandwich(String str) {
int breadCount=0;
for(int i=0;i<str.length();i++)
{

breadCount++;
}
if(breadCount>1){
return str.replaceAll(".*bread.*","");
}
else if(breadCount==1){
return "";
}
else
return "aaa";
}

Open in new window


why above fails below
Expected      Run            
getSandwich("breadjambread") → "jam"      ""      X         
getSandwich("xxbreadjambreadyy") → "jam"      ""      X         
getSandwich("xxbreadyy") → ""      ""      OK         
getSandwich("xxbreadbreadjambreadyy") → "breadjam"      ""      X         
getSandwich("breadAbread") → "A"      ""      X         
getSandwich("breadbread") → ""      ""      OK         
getSandwich("abcbreaz") → ""      "abcbreaz"      X         
getSandwich("xyz") → ""      "xyz"      X         
getSandwich("") → ""      "aaa"      X         
getSandwich("breadbreaxbread") → "breax"      ""      X         
getSandwich("breaxbreadybread") → "y"      ""      X         
getSandwich("breadbreadbreadbread") → "breadbread"      ""      X         
other tests

Author

Commented:
public String getSandwich(String str) {
int breadCount=0;
for(int i=0;i<str.length();i++)
{

breadCount++;
}
if(breadCount>1){
return str.replaceAll("bread","");
}
else if(breadCount==1){
return "";
}
else
return "aaa";
}

Open in new window


above fails some too

Expected      Run            
getSandwich("breadjambread") → "jam"      "jam"      OK         
getSandwich("xxbreadjambreadyy") → "jam"      "xxjamyy"      X         
getSandwich("xxbreadyy") → ""      "xxyy"      X         
getSandwich("xxbreadbreadjambreadyy") → "breadjam"      "xxjamyy"      X         
getSandwich("breadAbread") → "A"      "A"      OK         
getSandwich("breadbread") → ""      ""      OK         
getSandwich("abcbreaz") → ""      "abcbreaz"      X         
getSandwich("xyz") → ""      "xyz"      X         
getSandwich("") → ""      "aaa"      X         
getSandwich("breadbreaxbread") → "breax"      "breax"      OK         
getSandwich("breaxbreadybread") → "y"      "breaxy"      X         
getSandwich("breadbreadbreadbread") → "breadbread"      ""      X         
other tests
X
ozo
CERTIFIED EXPERT
Most Valuable Expert 2014
Top Expert 2015
Commented:
This one is on us!
(Get your first solution completely free - no credit card required)
UNLOCK SOLUTION

Author

Commented:
  return str.replaceFirst("(.*?bread(.*)bread)?.*","$2");

Open in new window


what is the logic and meaning of above solution
replacing all characters before(.*?bread) and after bread(bread)?.*",) with $2?
ozo
CERTIFIED EXPERT
Most Valuable Expert 2014
Top Expert 2015

Commented:
In a replacement string, $2 represents the string matched by the 2nd pair of capturing parentheses.
Information Technology Specialist
CERTIFIED EXPERT
Commented:
This one is on us!
(Get your first solution completely free - no credit card required)
UNLOCK SOLUTION
ozo
CERTIFIED EXPERT
Most Valuable Expert 2014
Top Expert 2015

Commented:
return str.indexOf("bread") == str.lastIndexOf("bread") ? "" :
       str.substring(str.indexOf("bread") + 5, str.lastIndexOf("bread"));

Open in new window

ozo
CERTIFIED EXPERT
Most Valuable Expert 2014
Top Expert 2015

Commented:
public String getSandwich(String str) {
    int first=Math.min(str.indexOf("bread")+5,str.length());
    int last=Math.max(str.lastIndexOf("bread"),first);
    return str.substring(first,last);
}

Open in new window

Author

Commented:
str.substring(str.indexOf("bread") + 5

why we are adding to 5

in below solution

public class HelloWorld{
     public static void main(String []args){
        System.out.println("Hello World"+ getSandwich("breadjambread"));
     }
     
     public  static String getSandwich(String str) {

return str.indexOf("bread") == str.lastIndexOf("bread") ? "" : str.substring(str.indexOf("bread") + 5, str.lastIndexOf("bread"));
}

}
ozo
CERTIFIED EXPERT
Most Valuable Expert 2014
Top Expert 2015

Commented:
why we are adding 5
To get from the index of the beginning of "bread" to the index of the end of "bread"

Author

Commented:
public class HelloWorld{
     public static void main(String []args){
        System.out.println("Hello World"+ getSandwich("breadAbread"));
     }
     
     public  static String getSandwich(String str) {
    	 System.out.println("  is-->"+ str.indexOf("bread"));
    	 System.out.println("  last is-->"+ str.lastIndexOf("bread"));

return str.indexOf("bread") == str.lastIndexOf("bread") ? "" 
		: str.substring(str.indexOf("bread") + 5, str.lastIndexOf("bread"));
}

}

Open in new window


above gave below right output.

  is-->0
  last is-->6
Hello WorldA


so we are checking indexOf ==lastIndex which means only one bread then return ""
otherwise we are basically asking to print middle sandwitched part using

str.substring(str.indexOf("bread") + 5, str.lastIndexOf("bread"))

Author

Commented:
public String getSandwich(String str) {
  return str.replaceFirst("(?:(?!bread).)*(?:bread(.*)bread)?.*","$1");
}

how to test above in regex101 site?

i should only take as bolded above by ignoring all the java statements?
ozo
CERTIFIED EXPERT
Most Valuable Expert 2014
Top Expert 2015

Commented:

Author

Commented:
why we need substitution variable as

$1

if i remove that how to re run
to see new output

please advise
ozo
CERTIFIED EXPERT
Most Valuable Expert 2014
Top Expert 2015

Commented:

Author

Commented:
as attached i am not able to see output now at bottom most section.


How to get output.

what is the button i have to click to see output?

https://regex101.com/r/gT1yH8/2
jam.png

Author

Commented:
what is meaning of $1 in substitution?
ozo
CERTIFIED EXPERT
Most Valuable Expert 2014
Top Expert 2015

Commented:
https://regex101.com/r/gT1yH8/3
Substitution
Contents in capture group 1  $1
This will return a string with the contents from the first capture group. The number, in this case 1, can be any number as long as it corresponds to a valid capture group.

Author

Commented:
what is difference between capturing group and substitution. are both same?
ozo
CERTIFIED EXPERT
Most Valuable Expert 2014
Top Expert 2015

Commented:
A capturing group is a pair of (parentheses) in the regular expression -- except when the pattern in the parentheses starts with ?

A substitution is the second argument in a replaceFirst(pattern,substitution) call in Java,
or what you enter in the Substitution input box in regex101.com

In a Java (or regex101) substitution, "$1" gets replaced by what was matched by the 1st capturing group.

Author

Commented:
if i remove that how to re run
to see new output

so there is no submit button ..looks like site itself is running and giving output automatically
Unlock the solution to this question.
Join our community and discover your potential

Experts Exchange is the only place where you can interact directly with leading experts in the technology field. Become a member today and access the collective knowledge of thousands of technology experts.

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.