Link to home
Start Free TrialLog in
Avatar of gudii9
gudii9Flag for United States of America

asked on

getSandwich java challenge

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
Avatar of ozo
ozo
Flag of United States of America image

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

ASKER

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
Avatar of gudii9

ASKER

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
SOLUTION
Avatar of ozo
ozo
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of gudii9

ASKER

  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?
In a replacement string, $2 represents the string matched by the 2nd pair of capturing parentheses.
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
return str.indexOf("bread") == str.lastIndexOf("bread") ? "" :
       str.substring(str.indexOf("bread") + 5, str.lastIndexOf("bread"));

Open in new window

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

Avatar of gudii9

ASKER

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

}
why we are adding 5
To get from the index of the beginning of "bread" to the index of the end of "bread"
Avatar of gudii9

ASKER

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"))
Avatar of gudii9

ASKER

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?
Avatar of gudii9

ASKER

why we need substitution variable as

$1

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

please advise
Avatar of gudii9

ASKER

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
Avatar of gudii9

ASKER

what is meaning of $1 in substitution?
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.
Avatar of gudii9

ASKER

what is difference between capturing group and substitution. are both same?
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.
Avatar of gudii9

ASKER

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