wordEnds java challenge

gudii9
gudii9 used Ask the Experts™
on
Hi,
I am working on below challenge
http://codingbat.com/prob/p147538
I tried my code as below
public String wordEnds(String str, String word) {
int xyPos=str.indexOf("xy");
String s1=str.substring(xyPos,xyPos-1);
String s2=str.replaceAll(str,word);
String s3=s1+s2;
return s3;
  
}

Open in new window

I am getting below result
Expected      Run            
wordEnds("abcXY123XYijk", "XY") → "c13i"      "Exception:java.lang.StringIndexOutOfBoundsException: String index out of range: -1 (line number:3)"      X         
wordEnds("XY123XY", "XY") → "13"      "Exception:java.lang.StringIndexOutOfBoundsException: String index out of range: -1 (line number:3)"      X         
wordEnds("XY1XY", "XY") → "11"      "Exception:java.lang.StringIndexOutOfBoundsException: String index out of range: -1 (line number:3)"      X         
wordEnds("XYXY", "XY") → "XY"      "Exception:java.lang.StringIndexOutOfBoundsException: String index out of range: -1 (line number:3)"      X         
wordEnds("XY", "XY") → ""      "Exception:java.lang.StringIndexOutOfBoundsException: String index out of range: -1 (line number:3)"      X         
wordEnds("Hi", "XY") → ""      "Exception:java.lang.StringIndexOutOfBoundsException: String index out of range: -1 (line number:3)"      X         
wordEnds("", "XY") → ""      "Exception:java.lang.StringIndexOutOfBoundsException: String index out of range: -1 (line number:3)"      X         
wordEnds("abc1xyz1i1j", "1") → "cxziij"      "Exception:java.lang.StringIndexOutOfBoundsException: String index out of range: -1 (line number:3)"      X         
wordEnds("abc1xyz1", "1") → "cxz"      "Exception:java.lang.StringIndexOutOfBoundsException: String index out of range: -1 (line number:3)"      X         
wordEnds("abc1xyz11", "1") → "cxz11"      "Exception:java.lang.StringIndexOutOfBoundsException: String index out of range: -1 (line number:3)"      X         
wordEnds("abc1xyz1abc", "abc") → "11"      "Exception:java.lang.StringIndexOutOfBoundsException: String index out of range: -1 (line number:3)"      X         
wordEnds("abc1xyz1abc", "b") → "acac"      "Exception:java.lang.StringIndexOutOfBoundsException: String index out of range: -1 (line number:3)"      X         
wordEnds("abc1abc1abc", "abc") → "1111"      "Exception:java.lang.StringIndexOutOfBoundsException: String index out of range: -1 (line number:3)"      X         
other tests
X         
Your progress graph for this problem

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

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Top Expert 2015

Commented:
Improvement to approach would be to do yourself instead of asking others to pass your exam.
ozo
Most Valuable Expert 2014
Top Expert 2015

Commented:
public String wordEnds(String str, String word) {
   return str.replaceAll("(?:(?!.?"+word+").)*(?:(.)?"+word+"(?=(.)"+word+")?"+"(?="+word+"(?<=(.)))?(.)?)?","$1$2$4$3");
}
// this could be easier using matcher.group() instead of replaceAll(), but I don't see a way to include java.util.regex.Matcher from codingbat

Author

Commented:
i was not clear on challenge

wordEnds("abcXY123XYijk", "XY") → "c13i"//this is where c and i are coming?
wordEnds("XY123XY", "XY") → "13"//how 13 came?
wordEnds("XY1XY", "XY") → "11"//how 11 came?
Ensure you’re charging the right price for your IT

Do you wonder if your IT business is truly profitable or if you should raise your prices? Learn how to calculate your overhead burden using our free interactive tool and use it to determine the right price for your IT services. Start calculating Now!

ozo
Most Valuable Expert 2014
Top Expert 2015

Commented:
wordEnds("abcXY123XYijk", "XY") → "c13i"//this is where c and i are coming?
this is where c and 1 are coming
i is coming from
wordEnds("abcXY123XYijk", "XY") → "c13i"

wordEnds("XY123XY", "XY") → "13"//how 13 came?
wordEnds("XY123XY", "XY") → "13"
wordEnds("XY123XY", "XY") → "13"

wordEnds("XY1XY", "XY") → "11"//how 11 came?
wordEnds("XY1XY", "XY") → "11"
wordEnds("XY1XY", "XY") → "11"

Author

Commented:
wordEnds("abcXY123XYijk", "XY") → "c13i"//this is where c and i are coming?
this is where c and 1 are coming
i is coming from
wordEnds("abcXY123XYijk", "XY") → "c13i"

why 2 is filtered out ??
if more than one word( XY) say two word(XY) as above which word(XY) we have to consider first XY or second XY to identify

return a string made of each char just before and just after every appearance of the word in the string. I

Author

Commented:
i think now i understand
we want each before each after characters for every occurence of word..

Author

Commented:
public class WordEndsEx {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub

       System.out.println("value is----"+wordEnds("abcXY123XYijk", "XY")); //"c13i"
	}

	
	public static String wordEnds(String str, String word) {
		int xyPos=str.indexOf("XY");
		int xyPosition=str.lastIndexOf("XY");
		System.out.println("xyPos"+xyPos);
		System.out.println("xyPosition"+xyPosition);
		String s1=str.substring(xyPos-1,xyPos);
		String s2=str.substring(xyPos+2,xyPos+3);
		//String s2=str.replaceAll(str,word);
		String s3=str.substring(xyPosition-1,xyPosition);
		String s4=str.substring(xyPosition+2,xyPosition+3);
		System.out.println("s1 is"+s1);
		System.out.println("s2 is"+s2);
		System.out.println("s3 is"+s3);
		System.out.println("s4 is"+s4);
		//String s4=str.replaceAll(str,word);
		//String s5=s1+s2;
		//String s6=s3+s4;
		String s7=s1+s2+s3+s4;
		return s7;
		  
		}
}

Open in new window


above gave below correct result.

xyPos3
xyPosition8
s1 isc
s2 is1
s3 is3
s4 isi
value is----c13i


how to improve my code above?
please advise

Author

Commented:
public String wordEnds(String str, String word) {
int xyPos=str.indexOf("XY");
		int xyPosition=str.lastIndexOf("XY");
		//System.out.println("xyPos"+xyPos);
		//System.out.println("xyPosition"+xyPosition);
		String s1=str.substring(xyPos-1,xyPos);
		String s2=str.substring(xyPos+2,xyPos+3);
		//String s2=str.replaceAll(str,word);
		String s3=str.substring(xyPosition-1,xyPosition);
		String s4=str.substring(xyPosition+2,xyPosition+3);
		//System.out.println("s1 is"+s1);
		//System.out.println("s2 is"+s2);
		//System.out.println("s3 is"+s3);
		//System.out.println("s4 is"+s4);
		//String s4=str.replaceAll(str,word);
		//String s5=s1+s2;
		//String s6=s3+s4;
		String s7=s1+s2+s3+s4;
		return s7;
  
}

Open in new window


above failing test cases as below.
Expected	Run		
wordEnds("abcXY123XYijk", "XY") → "c13i"	"c13i"	OK	    
wordEnds("XY123XY", "XY") → "13"	"Exception:java.lang.StringIndexOutOfBoundsException: String index out of range: -1 (line number:6)"	X	    
wordEnds("XY1XY", "XY") → "11"	"Exception:java.lang.StringIndexOutOfBoundsException: String index out of range: -1 (line number:6)"	X	    
wordEnds("XYXY", "XY") → "XY"	"Exception:java.lang.StringIndexOutOfBoundsException: String index out of range: -1 (line number:6)"	X	    
wordEnds("XY", "XY") → ""	"Exception:java.lang.StringIndexOutOfBoundsException: String index out of range: -1 (line number:6)"	X	    
wordEnds("Hi", "XY") → ""	"Exception:java.lang.StringIndexOutOfBoundsException: String index out of range: -2 (line number:6)"	X	    
wordEnds("", "XY") → ""	"Exception:java.lang.StringIndexOutOfBoundsException: String index out of range: -2 (line number:6)"	X	    
wordEnds("abc1xyz1i1j", "1") → "cxziij"	"Exception:java.lang.StringIndexOutOfBoundsException: String index out of range: -2 (line number:6)"	X	    
wordEnds("abc1xyz1", "1") → "cxz"	"Exception:java.lang.StringIndexOutOfBoundsException: String index out of range: -2 (line number:6)"	X	    
wordEnds("abc1xyz11", "1") → "cxz11"	"Exception:java.lang.StringIndexOutOfBoundsException: String index out of range: -2 (line number:6)"	X	    
wordEnds("abc1xyz1abc", "abc") → "11"	"Exception:java.lang.StringIndexOutOfBoundsException: String index out of range: -2 (line number:6)"	X	    
wordEnds("abc1xyz1abc", "b") → "acac"	"Exception:java.lang.StringIndexOutOfBoundsException: String index out of range: -2 (line number:6)"	X	    
wordEnds("abc1abc1abc", "abc") → "1111"	"Exception:java.lang.StringIndexOutOfBoundsException: String index out of range: -2 (line number:6)"	X	    
other tests
X	 

Open in new window

please advise

Author

Commented:
public String wordEnds(String str, String word) {
int xyPos=str.indexOf("XY");
		int xyPosition=str.lastIndexOf("XY");
		//System.out.println("xyPos"+xyPos);
		//System.out.println("xyPosition"+xyPosition);
if(xyPos>=1 && xyPosition>=1){
		String s1=str.substring(xyPos-1,xyPos);
		String s2=str.substring(xyPos+2,xyPos+3);
		//String s2=str.replaceAll(str,word);
		String s3=str.substring(xyPosition-1,xyPosition);
		String s4=str.substring(xyPosition+2,xyPosition+3);
		//System.out.println("s1 is"+s1);
		//System.out.println("s2 is"+s2);
		//System.out.println("s3 is"+s3);
		//System.out.println("s4 is"+s4);
		//String s4=str.replaceAll(str,word);
		//String s5=s1+s2;
		//String s6=s3+s4;
		String s7=s1+s2+s3+s4;
		return s7;
}
else
return "hello";
  
}

Open in new window

Expected      Run            
wordEnds("abcXY123XYijk", "XY") → "c13i"      "c13i"      OK         
wordEnds("XY123XY", "XY") → "13"      "hello"      X         
wordEnds("XY1XY", "XY") → "11"      "hello"      X         
wordEnds("XYXY", "XY") → "XY"      "hello"      X         
wordEnds("XY", "XY") → ""      "hello"      X         
wordEnds("Hi", "XY") → ""      "hello"      X         
wordEnds("", "XY") → ""      "hello"      X         
wordEnds("abc1xyz1i1j", "1") → "cxziij"      "hello"      X         
wordEnds("abc1xyz1", "1") → "cxz"      "hello"      X         
wordEnds("abc1xyz11", "1") → "cxz11"      "hello"      X         
wordEnds("abc1xyz1abc", "abc") → "11"      "hello"      X         
wordEnds("abc1xyz1abc", "b") → "acac"      "hello"      X         
wordEnds("abc1abc1abc", "abc") → "1111"      "hello"      X         
other tests
X

how to fix above tests.

Author

Commented:
public class WordEndsEx {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub

      // System.out.println("value is----"+wordEnds("abcXY123XYijk", "XY")); //"c13i"
       System.out.println("value is----"+wordEnds("XY123XY", "XY")); //"13"
	}

	
	public static String wordEnds(String str, String word) {
	int xyPos=str.indexOf("XY");
	int xyPosition=str.lastIndexOf("XY");
	//System.out.println("xyPos"+xyPos);
	//System.out.println("xyPosition"+xyPosition);
if(xyPos>=1 && xyPosition>=1){
	String s1=str.substring(xyPos-1,xyPos);
	String s2=str.substring(xyPos+2,xyPos+3);
	//String s2=str.replaceAll(str,word);
	String s3=str.substring(xyPosition-1,xyPosition);
	String s4=str.substring(xyPosition+2,xyPosition+3);
	//System.out.println("s1 is"+s1);
	//System.out.println("s2 is"+s2);
	//System.out.println("s3 is"+s3);
	//System.out.println("s4 is"+s4);
	//String s4=str.replaceAll(str,word);
	//String s5=s1+s2;
	//String s6=s3+s4;
	String s7=s1+s2+s3+s4;
	return s7;
}
else if(xyPos==0 && (    xyPosition>=1&&(  (str.length()-xyPosition >=3)) )    ){
	String s1="";
	System.out.println("s1 is-->"+s1);
	String s2=str.substring(xyPos+2,xyPos+3);
	System.out.println("s2 is-->"+s2);
	//String s2=str.replaceAll(str,word);
	
	String s3=str.substring(xyPosition-1,xyPosition);
	System.out.println("s3 is-->"+s3);
	String s4=str.substring(xyPosition+2,xyPosition+3);
	System.out.println("s4 is-->"+s4);
	//System.out.println("s1 is"+s1);
	//System.out.println("s2 is"+s2);
	//System.out.println("s3 is"+s3);
	//System.out.println("s4 is"+s4);
	//String s4=str.replaceAll(str,word);
	//String s5=s1+s2;
	//String s6=s3+s4;
	String s7=s1+s2+s3+s4;
	return s7;
}


else if(xyPos>=1 && xyPosition==0){
	String s1=str.substring(xyPos-1,xyPos);
	String s2=str.substring(xyPos+2,xyPos+3);
	//String s2=str.replaceAll(str,word);
	String s3=str.substring(xyPosition-1,xyPosition);
	String s4="";
	//System.out.println("s1 is"+s1);
	//System.out.println("s2 is"+s2);
	//System.out.println("s3 is"+s3);
	//System.out.println("s4 is"+s4);
	//String s4=str.replaceAll(str,word);
	//String s5=s1+s2;
	//String s6=s3+s4;
	String s7=s1+s2+s3+s4;
	return s7;
}

else
return "hello";}
}

Open in new window


tried like above but not working

Author

Commented:
public class WordEndsEx {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub

      // System.out.println("value is----"+wordEnds("abcXY123XYijk", "XY")); //"c13i"
     //  System.out.println("value is----"+wordEnds("XY123XY", "XY")); //"13"
       //System.out.println("value is----"+wordEnds("hi", "XY")); //""
       System.out.println("value is---"+wordEnds("abc1xyz1i1j", "1"));//"cxziij"
	}

	
	public static String wordEnds(String str, String word) {
		int xyPos=str.indexOf(word);
	int xyPosition=str.lastIndexOf(word);
	System.out.println("xyPos"+xyPos);
	System.out.println("xyPosition"+xyPosition);
if(xyPos>=1 && xyPosition>=1){
	String s1=str.substring(xyPos-1,xyPos);
	String s2;
	if(str.length()-xyPosition>=3){
	s2=str.substring(xyPos+2,xyPos+3);
	}
	else{
		s2="";
	}
		
	//String s2=str.replaceAll(str,word);
	String s3=str.substring(xyPosition-1,xyPosition);
	//String s4=str.substring(xyPosition+2,xyPosition+3);
	String s4;
	if(str.length()-xyPosition>=3){
	s4=str.substring(xyPos+2,xyPos+3);
	}
	else{
		s4="";
	}
	//System.out.println("s1 is"+s1);
	//System.out.println("s2 is"+s2);
	//System.out.println("s3 is"+s3);
	//System.out.println("s4 is"+s4);
	//String s4=str.replaceAll(str,word);
	//String s5=s1+s2;
	//String s6=s3+s4;
	String s7=s1+s2+s3+s4;
	return s7;
}
else if(xyPos<=0 && xyPosition>=1){
	String s1="";
	//System.out.println("s1 is-->"+s1);
	String s2=str.substring(xyPos+2,xyPos+3);
	//System.out.println("s2 is-->"+s2);
	//String s2=str.replaceAll(str,word);
	
	String s3=str.substring(xyPosition-1,xyPosition);
	//System.out.println("s3 is-->"+s3);
	String s4;
	if(str.length()-xyPosition >=3){
	s4=str.substring(xyPosition+2,xyPosition+3);
	}else{
		s4="";
	}
	//System.out.println("s4 is-->"+s4);
	//System.out.println("s1 is"+s1);
	//System.out.println("s2 is"+s2);
	//System.out.println("s3 is"+s3);
	//System.out.println("s4 is"+s4);
	//String s4=str.replaceAll(str,word);
	//String s5=s1+s2;
	//String s6=s3+s4;
	String s7=s1+s2+s3+s4;
	return s7;
}


else if(xyPos>=1 && xyPosition<=0){
	String s1=str.substring(xyPos-1,xyPos);
	String s2=str.substring(xyPos+2,xyPos+3);
	//String s2=str.replaceAll(str,word);
	String s3=str.substring(xyPosition-1,xyPosition);
	String s4="";
	//System.out.println("s1 is"+s1);
	//System.out.println("s2 is"+s2);
	//System.out.println("s3 is"+s3);
	//System.out.println("s4 is"+s4);
	//String s4=str.replaceAll(str,word);
	//String s5=s1+s2;
	//String s6=s3+s4;
	String s7=s1+s2+s3+s4;
	return s7;
}


else if(xyPos<=0 && xyPosition<=0){
	System.out.println("here");
	return "";
}

else
return "hello";}
}

Open in new window


i got wrong output
xyPos3
xyPosition9
value is---ci
public String wordEnds(String str, String word) {

	int xyPos=str.indexOf("XY");
	int xyPosition=str.lastIndexOf("XY");
	//System.out.println("xyPos"+xyPos);
	//System.out.println("xyPosition"+xyPosition);
       if(xyPos>=1 && xyPosition>=1){
	String s1=str.substring(xyPos-1,xyPos);
	String s2=str.substring(xyPos+2,xyPos+3);
	//String s2=str.replaceAll(str,word);
	String s3=str.substring(xyPosition-1,xyPosition);
	String s4=str.substring(xyPosition+2,xyPosition+3);
	//System.out.println("s1 is"+s1);
	//System.out.println("s2 is"+s2);
	//System.out.println("s3 is"+s3);
	//System.out.println("s4 is"+s4);
	//String s4=str.replaceAll(str,word);
	//String s5=s1+s2;
	//String s6=s3+s4;
	String s7=s1+s2+s3+s4;
	return s7;
}
else if(xyPos<=0 && xyPosition>=1){
	String s1="";
	//System.out.println("s1 is-->"+s1);
	String s2=str.substring(xyPos+2,xyPos+3);
	//System.out.println("s2 is-->"+s2);
	//String s2=str.replaceAll(str,word);
	
	String s3=str.substring(xyPosition-1,xyPosition);
	//System.out.println("s3 is-->"+s3);
	String s4;
	if(str.length()-xyPosition >=3){
	s4=str.substring(xyPosition+2,xyPosition+3);
	}else{
		s4="";
	}
	//System.out.println("s4 is-->"+s4);
	//System.out.println("s1 is"+s1);
	//System.out.println("s2 is"+s2);
	//System.out.println("s3 is"+s3);
	//System.out.println("s4 is"+s4);
	//String s4=str.replaceAll(str,word);
	//String s5=s1+s2;
	//String s6=s3+s4;
	String s7=s1+s2+s3+s4;
	return s7;
}


else if(xyPos>=1 && xyPosition<=0){
	String s1=str.substring(xyPos-1,xyPos);
	String s2=str.substring(xyPos+2,xyPos+3);
	//String s2=str.replaceAll(str,word);
	String s3=str.substring(xyPosition-1,xyPosition);
	String s4="";
	//System.out.println("s1 is"+s1);
	//System.out.println("s2 is"+s2);
	//System.out.println("s3 is"+s3);
	//System.out.println("s4 is"+s4);
	//String s4=str.replaceAll(str,word);
	//String s5=s1+s2;
	//String s6=s3+s4;
	String s7=s1+s2+s3+s4;
	return s7;
}


else if(xyPos<=0 && xyPosition<=0){
	
	return "";
}

else
return "hello";
  
}

Open in new window


Expected      Run            
wordEnds("abcXY123XYijk", "XY") → "c13i"      "c13i"      OK         
wordEnds("XY123XY", "XY") → "13"      "13"      OK         
wordEnds("XY1XY", "XY") → "11"      "11"      OK         
wordEnds("XYXY", "XY") → "XY"      "XY"      OK         
wordEnds("XY", "XY") → ""      ""      OK         
wordEnds("Hi", "XY") → ""      ""      OK         
wordEnds("", "XY") → ""      ""      OK         
wordEnds("abc1xyz1i1j", "1") → "cxziij"      ""      X         
wordEnds("abc1xyz1", "1") → "cxz"      ""      X         
wordEnds("abc1xyz11", "1") → "cxz11"      ""      X         
wordEnds("abc1xyz1abc", "abc") → "11"      ""      X         
wordEnds("abc1xyz1abc", "b") → "acac"      ""      X         
wordEnds("abc1abc1abc", "abc") → "1111"      ""      X         
other tests
X         
Your progress graph for this problem

please advise

Author

Commented:
how to write without using regular expression?
Most Valuable Expert 2014
Top Expert 2015
Commented:
public String wordEnds(String str, String word) {
  String ends="";
  for( int i=0; i<str.length(); ++i ){
     if( str.startsWith(word,i) ){
       if( i>0 ){
          ends += str.substring(i-1,i);
       }
       if( i+word.length()<str.length() ){
           ends += str.substring(i+word.length(),i+word.length()+1);
       }
     }
  }
  return ends;
}

Author

Commented:
let me understand

Author

Commented:
public class WordEnds {

	public static void main(String[] args) {
		// TODO Auto-generated method stub

		/*
		 * Given a string and a non-empty word string, return a string made of
		 * each char just before and just after every appearance of the word in
		 * the string. Ignore cases where there is no char before or after the
		 * word, and a char may be included twice if it is between two words.
		 * 
		 * wordEnds("abcXY123XYijk", "XY") → "c13i" wordEnds("XY123XY", "XY") →
		 * "13" wordEnds("XY1XY", "XY") → "11"
		 */
		System.out.println("is--->" + wordEnds("abcXY123XYijk", "XY"));

	}

	public static String wordEnds(String str, String word) {
		String ends = "";
		for (int i = 0; i < str.length(); ++i) {
			if (str.startsWith(word, i)) {
				if (i > 0) {
					ends += str.substring(i - 1, i);
					System.out.println("end is--->"+ends);
				}
				if (i + word.length() < str.length()) {
					ends += str.substring(i + word.length(), i + word.length() + 1);
					System.out.println("end isss--->"+ends);
				}
			}
		}
		return ends;
	}

}

Open in new window


above gave below output
end is--->c
end isss--->c1
end is--->c13
end isss--->c13i
is--->c13i

Author

Commented:
when i try to debug it is strting with i=3 not i=0 for unknown reason. please advise
IValue.jpg
ozo
Most Valuable Expert 2014
Top Expert 2015

Commented:
when str="abcXY123XYijk", word="XY", and i=0,
what is str.startsWith(word, i)?

Author

Commented:
when str="abcXY123XYijk", word="XY", and i=0,
what is str.startsWith(word, i)?

true right as XY is at index 3 but i stilll though eclipse debugger should have strted with i=0 not i=3. please advise
ozo
Most Valuable Expert 2014
Top Expert 2015

Commented:
If you are looking at line 25, you don't  get to line 25 when i=0

Author

Commented:
If you are looking at line 25, you don't  get to line 25 when i=0

so ariable values on right hand side specific to a line number like 25?

i though i got assigned some value at line 23 which should carry forward?

please advise
ozo
Most Valuable Expert 2014
Top Expert 2015

Commented:
i got assigned the value 0 in line 22
when i was 0, line 25 was not executed
then i got incremeted from 0 to 1 in line 22
when i was 1, line 25 was not executed
then i got incremeted from 1 to 2 in line 22
when i was 2, line 25 was not executed
then i got incremeted from 2 to 3 in line 22
when i was 3, line 25 was executed, and the value of i at that time was 3.

Author

Commented:
by putting break point at 22 i can see right

i got assigned the value 0 in line 22
when i was 0, line 25 was not executed
then i got changed from 0 to 1 in line 22
when i was 1, line 25 was not executed
then i got changed from 1 to 2 in line 22
when i was 2, line 25 was not executed
then i got changed from 2 to 3 in line 22
when i was 3, line 25 was executed, and the value of i at that time was 3.

Author

Commented:
public String wordEnds(String str, String word) {
   return str.replaceAll("(?:(?!.?"+word+").)*(?:(.)?"+word+"(?=(.)"+word+")?"+"(?="+word+"(?<=(.)))?(.)?)?","$1$2$4$3");
}
// this could be easier using matcher.group() instead of replaceAll(), but I don't see a way to include java.util.regex.Matcher from codingbat

can you please advise on above. I was not clear what is going on?
ozo
Most Valuable Expert 2014
Top Expert 2015

Commented:
I don't think that's a good way to do it, it's too awkward to reconcile the way the challenge wants to handle overlaps within the way replaceAll wants to handle them.
(it might be a little easier if there was a character that was guaranteed to never occur in str, but it's still probably easier to do this challenge with a loop)

Author

Commented:
if (i + word.length() < str.length()

i wonder why we are checking above if condition?

i see
for loop has one top level if which has two bottom  level if loops which are filling before and after character of xy ie word

      if (i > 0) {
                              ends += str.substring(i - 1, i);
                              System.out.println("end is--->"+ends);
                        }
                        if (i + word.length() < str.length()) {
                              ends += str.substring(i + word.length(), i + word.length() + 1);
                              System.out.println("end isss--->"+ends);
                        }

public class WordEnds {

	public static void main(String[] args) {
		// TODO Auto-generated method stub

		/*
		 * Given a string and a non-empty word string, return a string made of
		 * each char just before and just after every appearance of the word in
		 * the string. Ignore cases where there is no char before or after the
		 * word, and a char may be included twice if it is between two words.
		 * 
		 * wordEnds("abcXY123XYijk", "XY") → "c13i" wordEnds("XY123XY", "XY") →
		 * "13" wordEnds("XY1XY", "XY") → "11"
		 */
		System.out.println("is--->" + wordEnds("abcXY123XYijk", "XY"));

	}

	public static String wordEnds(String str, String word) {
		String ends = "";
		for (int i = 0; i < str.length(); ++i) {
			if (str.startsWith(word, i)) {
				if (i > 0) {
					ends += str.substring(i - 1, i);
					System.out.println("end is--->"+ends);
				}
				if (i + word.length() < str.length()) {
					ends += str.substring(i + word.length(), i + word.length() + 1);
					System.out.println("end isss--->"+ends);
				}
			}
		}
		return ends;
	}

}

Open in new window

Author

Commented:
public class WordEnds {

	public static void main(String[] args) {
		// TODO Auto-generated method stub

		/*
		 * Given a string and a non-empty word string, return a string made of
		 * each char just before and just after every appearance of the word in
		 * the string. Ignore cases where there is no char before or after the
		 * word, and a char may be included twice if it is between two words.
		 * 
		 * wordEnds("abcXY123XYijk", "XY") → "c13i" wordEnds("XY123XY", "XY") →
		 * "13" wordEnds("XY1XY", "XY") → "11"
		 */
		System.out.println("is--->" + wordEnds("abcXY123XYijk", "XY"));

	}

	public static String wordEnds(String str, String word) {
		String ends = "";
		for (int i = 0; i < str.length(); ++i) {
			if (str.startsWith(word, i)) {
				if (i > 0) {
					ends += str.substring(i - 1, i);
					System.out.println("end is--->"+ends);
				}
				if (i > 0) {//here i changed condition as i>0
					ends += str.substring(i + word.length(), i + word.length() + 1);
					System.out.println("end isss--->"+ends);
				}
			}
		}
		return ends;
	}

}

Open in new window


above also worked fine gave same output
end is--->c
end isss--->c1
end is--->c13
end isss--->c13i
is--->c13i
ozo
Most Valuable Expert 2014
Top Expert 2015

Commented:
with
if (i > 0) {//here i changed condition as i>0
Expected      Run
wordEnds("XY123XY", "XY") → "13"      "Exception:java.lang.StringIndexOutOfBoundsException: String index out of range: 8 (line number:10)"

Author

Commented:
public class WordEnds {

	public static void main(String[] args) {
		// TODO Auto-generated method stub

		/*
		 * Given a string and a non-empty word string, return a string made of
		 * each char just before and just after every appearance of the word in
		 * the string. Ignore cases where there is no char before or after the
		 * word, and a char may be included twice if it is between two words.
		 * 
		 * wordEnds("abcXY123XYijk", "XY") → "c13i" wordEnds("XY123XY", "XY") →
		 * "13" wordEnds("XY1XY", "XY") → "11"
		 */
		System.out.println("is--->" + wordEnds("XY123XY", "XY"));

	}

	public static String wordEnds(String str, String word) {
		String ends = "";
		for (int i = 0; i < str.length(); ++i) {
			if (str.startsWith(word, i)) {
				if (i > 0) {
					ends += str.substring(i - 1, i);
					System.out.println("end is--->"+ends);
				}
				if (i > 0) {//here i changed condition as i>0
					ends += str.substring(i + word.length(), i + word.length() + 1);
					System.out.println("end isss--->"+ends);
				}
			}
		}
		return ends;
	}

}

Open in new window


correct above failed with below message
end is--->3
Exception in thread "main" java.lang.StringIndexOutOfBoundsException
      at java.lang.String.substring(String.java:1148)
      at WordEnds.wordEnds(WordEnds.java:28)
      at WordEnds.main(WordEnds.java:15)

Author

Commented:
so below is needed in bottome if to pass all tests?

if(i + word.length() < str.length()) not if if (i > 0)
public class WordEnds {

	public static void main(String[] args) {
		// TODO Auto-generated method stub

		/*
		 * Given a string and a non-empty word string, return a string made of
		 * each char just before and just after every appearance of the word in
		 * the string. Ignore cases where there is no char before or after the
		 * word, and a char may be included twice if it is between two words.
		 * 
		 * wordEnds("abcXY123XYijk", "XY") → "c13i" wordEnds("XY123XY", "XY") →
		 * "13" wordEnds("XY1XY", "XY") → "11"
		 */
		System.out.println("is--->" + wordEnds("XY123XY", "XY"));

	}

	public static String wordEnds(String str, String word) {
		String ends = "";
		for (int i = 0; i < str.length(); ++i) {
			if (str.startsWith(word, i)) {
				if (i > 0) {
					ends += str.substring(i - 1, i);
					System.out.println("end is--->"+ends);
				}
				if (i > 0) {//here i changed condition as i>0 not if(i + word.length() < str.length())
					ends += str.substring(i + word.length(), i + word.length() + 1);
					System.out.println("end isss--->"+ends);
				}
			}
		}
		return ends;
	}

}

Open in new window

Author

Commented:
public class WordEnds {

	public static void main(String[] args) {
		// TODO Auto-generated method stub

		/*
		 * Given a string and a non-empty word string, return a string made of
		 * each char just before and just after every appearance of the word in
		 * the string. Ignore cases where there is no char before or after the
		 * word, and a char may be included twice if it is between two words.
		 * 
		 * wordEnds("abcXY123XYijk", "XY") → "c13i" wordEnds("XY123XY", "XY") →
		 * "13" wordEnds("XY1XY", "XY") → "11"
		 */
		System.out.println("is--->" + wordEnds("XY123XY", "XY"));

	}

	public static String wordEnds(String str, String word) {
		String ends = "";
		for (int i = 0; i < str.length(); ++i) {
			if (str.startsWith(word, i)) {
				if (i > 0) {
					ends += str.substring(i - 1, i);
					System.out.println("end is--->"+ends);
				}
				if(i + word.length() < str.length()) {//here i changed condition as i>0 not if(i + word.length() < str.length())
					ends += str.substring(i + word.length(), i + word.length() + 1);
					System.out.println("end isss--->"+ends);
				}
			}
		}
		return ends;
	}

}

Open in new window


above passed that test
end isss--->1
end is--->13
is--->13

Author

Commented:
wordEnds("XY123XY", "XY"));

wordEnds("abcXY123XYijk", "XY")

what is difference between above two. why first one failed with if (i > 0) { at bottom if loop?

Author

Commented:
if(i + word.length() < str.length()

what is meaning of above line?
ozo
Most Valuable Expert 2014
Top Expert 2015

Commented:
It allows
ends += str.substring(i + word.length(), i + word.length() + 1);
to executed only when str.substring(i + word.length(), i + word.length() + 1) exists

Author

Commented:
http://www.tutorialspoint.com/java/java_string_startswith.htm

i see you are using method with offset

public class WordEnds {

      public static void main(String[] args) {
            // TODO Auto-generated method stub

            /*
             * Given a string and a non-empty word string, return a string made of
             * each char just before and just after every appearance of the word in
             * the string. Ignore cases where there is no char before or after the
             * word, and a char may be included twice if it is between two words.
             *
             * wordEnds("abcXY123XYijk", "XY") → "c13i" wordEnds("XY123XY", "XY") →
             * "13" wordEnds("XY1XY", "XY") → "11"
             */
            System.out.println("is--->" + wordEnds("abc1xyz1i1j", "1"));// abcXY123XYijk

      }

      public static String wordEnds(String str, String word) {
              String ends="";
              for( int i=0; i<str.length(); i++ ){
                 if( str.startsWith(word,i) ){
                   if( i>0 ){
                      ends += str.substring(i-1,i);
                   }
                   if( i+word.length()<str.length() ){
                       ends += str.substring(i+word.length(),i+word.length()+1);
                   }
                 }
              }
              return ends;
            }

}

above gave same output as below even after changing ++i to i++

public class WordEnds {

	public static void main(String[] args) {
		// TODO Auto-generated method stub

		/*
		 * Given a string and a non-empty word string, return a string made of
		 * each char just before and just after every appearance of the word in
		 * the string. Ignore cases where there is no char before or after the
		 * word, and a char may be included twice if it is between two words.
		 * 
		 * wordEnds("abcXY123XYijk", "XY") → "c13i" wordEnds("XY123XY", "XY") →
		 * "13" wordEnds("XY1XY", "XY") → "11"
		 */
		System.out.println("is--->" + wordEnds("abc1xyz1i1j", "1"));// abcXY123XYijk

	}

	public static String wordEnds(String str, String word) {
		  String ends="";
		  for( int i=0; i<str.length(); i++ ){
		     if( str.startsWith(word,i) ){
		       if( i>0 ){
		          ends += str.substring(i-1,i);
		       }
		       if( i+word.length()<str.length() ){
		           ends += str.substring(i+word.length(),i+word.length()+1);
		       }
		     }
		  }
		  return ends;
		}

}

Open in new window


is--->cxziij


can i use i++ instead of ++i .

please advise

Author

Commented:
public class WordEnds {

	public static void main(String[] args) {
		// TODO Auto-generated method stub

		/*
		 * Given a string and a non-empty word string, return a string made of
		 * each char just before and just after every appearance of the word in
		 * the string. Ignore cases where there is no char before or after the
		 * word, and a char may be included twice if it is between two words.
		 * 
		 * wordEnds("abcXY123XYijk", "XY") → "c13i" wordEnds("XY123XY", "XY") →
		 * "13" wordEnds("XY1XY", "XY") → "11"
		 */
		System.out.println("is--->" + wordEnds("abc1xyz1i1j", "1"));// abcXY123XYijk

	}

	public static String wordEnds(String str, String word) {
		  String ends="";
		  for( int i=0; i<str.length(); i++ ){
		     if( str.startsWith(word,i) ){
		       if( i>0 ){
		          ends += str.substring(i-1,i);
		       }
		       if( i+word.length()<str.length() ){//3+1<11
		           ends += str.substring(i+word.length(),i+word.length()+1);//3+1,3+1+1
		       }
		     }
		  }
		  return ends;
		}

}

Open in new window


above fails below
Expected      Run            
wordEnds("abcXY123XYijk", "XY") → "c13i"      "c13i"      OK         
wordEnds("XY123XY", "XY") → "13"      "13"      OK         
wordEnds("XY1XY", "XY") → "11"      "11"      OK         
wordEnds("XYXY", "XY") → "XY"      "XY"      OK         
wordEnds("XY", "XY") → ""      ""      OK         
wordEnds("Hi", "XY") → ""      ""      OK         
wordEnds("", "XY") → ""      ""      OK         
wordEnds("abc1xyz1i1j", "1") → "cxziij"      ""      X         
wordEnds("abc1xyz1", "1") → "cxz"      ""      X         
wordEnds("abc1xyz11", "1") → "cxz11"      ""      X         
wordEnds("abc1xyz1abc", "abc") → "11"      ""      X         
wordEnds("abc1xyz1abc", "b") → "acac"      ""      X         
wordEnds("abc1abc1abc", "abc") → "1111"      ""      X         
other tests
X         
Your progress graph for this problem

Author

Commented:
It allows
ends += str.substring(i + word.length(), i + word.length() + 1);
to executed only when str.substring(i + word.length(), i + word.length() + 1) exists

when str.substring(i + word.length(), i + word.length() + 1) does not exist?
ozo
Most Valuable Expert 2014
Top Expert 2015

Commented:
when str.substring(i + word.length(), i + word.length() + 1) does not exist, trying to access it would cause java.lang.StringIndexOutOfBoundsException

Author

Commented:
It mean by does not exist?
ozo
Most Valuable Expert 2014
Top Expert 2015

Commented:
What would "XY".substring(3,4) be?

Author

Commented:
"XY".substring(3,4) be?  means stringoutofbound exception as after index1 no more characters in the string xy

Author

Commented:
str.substring(i+word.length(),i+word.length()+1)

i see str or xy length if greater than i+word.length(),i+word.length()+1 ie 3,4 for example then only we cando substring..

Author

Commented:
can i use i++ instead of ++i .
Does it make any difference here?
please advise.
ozo
Most Valuable Expert 2014
Top Expert 2015

Commented:
i++ instead of ++i makes no difference between
for( int i=0; i<str.length(); i++ ){
and
for( int i=0; i<str.length(); ++i ){

Author

Commented:
i++ instead of ++i makes no difference between
for( int i=0; i<str.length(); i++ ){
and
for( int i=0; i<str.length(); ++i ){

just here or any where?
ozo
Most Valuable Expert 2014
Top Expert 2015

Commented:
When the resulting value is not used.

Author

Commented:
When the resulting value is not used.
here we are using i within if condition right?
ozo
Most Valuable Expert 2014
Top Expert 2015

Commented:
The value of i is used in i<str.length();
The value of i++ is not used.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial