1

This is my task: Given a string, does "xyz" appear in the middle of the string? To define middle, we'll say that the number of chars to the left and right of the "xyz" must differ by at most one.

The problem description and the failures in others use case can be seen by using the code below here

xyzMiddle("AAxyzBB") → true

xyzMiddle("AxyzBB") → true

xyzMiddle("AxyzBBB") → false

My solution is below. Since I can't see what 'other tests' are, please help me spot the problem. My method is to check if 'y' appears in the middle for odd or even a String.

public boolean xyzMiddle(String str) {
  if (str.indexOf("xyz") < 0) return false;
  int l = str.length();
  int m = l / 2;
  if (l % 2 != 0) {
   if (str.charAt(m) != 'y') return false;
  }
  else {
   if (str.charAt(m) != 'y' && str.charAt(m - 1) != 'y') return false;
  }
  return true; 
}

2 Answers 2

0

The problem with your solution is that you are simply returning false in case the string in question has odd length

Which is actually not true, the pass use-cases for this task can be mathematically divided like below:

1)With xyz present in the middle and there is a string of length x + 1 and x to either the left or right of it.

taking length of string xyz as 3 the total length comes out to be:

(x) + 3 + (x + 1) = 2x + 4 --->Always even so in the case above we just check is xyz is in the middle or not and return accordingly which is already handled in your code.

2) With xyz present in the middle and there are strings of length x to the left or right of it.

Again taking length of string xyz as 3 the total length comes out to be:

(x) + 3 + (x) = 2x + 3 --->Always odd

Hence as per your solution to return true in this case(last line of code) you need to filter out the cases when length is odd but xyz is not in the middle as below:

if (!(str.substring((m - 1), m + 2).equals("xyz"))) 
   return false;

With this included your solutions looks like as below:

public boolean xyzMiddle(String str) {
  if (str.indexOf("xyz") < 0) return false;
  int l = str.length();
  int m = l / 2;
  if (l % 2 != 0) {
   if (!(str.substring((m - 1), m + 2).equals("xyz"))) 
   return false;
  }
  else {
   if (str.charAt(m) != 'y' && str.charAt(m - 1) != 'y') return false;
  }
  return true; 
}

Now it passes all the tests on codingBat.

2
  • Got it! Input like "xyzyabc" could give wrong result in my code. Thanks.
    – Mai Nguyen
    May 22, 2015 at 14:15
  • I believe it's AAxyzBB or AxyzA which could as explained in my answer as these are odd length strings.
    – Anirudh
    May 22, 2015 at 14:44
0
if(str.length() <3) return false;

   int ind = str.indexOf("xyz", str.length()/2 - 3) ;
   String first = str.substring(0, ind);
   String second = str.substring(ind+3);

  return (first.length() == second.length() || first.length() +1 == second.length() || first.length() == second.length() + 1);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.