Java > String-2 > xyzMiddle (CodingBat Solution)

Problem:

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. This problem is harder than it looks.

xyzMiddle("AAxyzBB") → true
xyzMiddle("AxyzBB") → true
xyzMiddle("AxyzBBB") → false


Solution:

public boolean xyzMiddle(String str) {
  String xyz = "xyz";
  int len = str.length();
  int middle = len / 2;
  
  if (len < 3)
    return false;
    
  if (len % 2 != 0) {
    if (xyz.equals(str.substring(middle-1,middle+2))) {
      return true;
    } else {
        return false;
      }
  } else if (xyz.equals(str.substring(middle-1,middle+2)) || 
      xyz.equals(str.substring(middle-2,middle+1))) {
          return true;
  } else
      return false;  
}


20 comments :

  1. public boolean xyzMiddle(String str) {
    if(str.length()<3) return false;
    int m = str.length()/2;
    if(str.charAt(m-1)=='y' && str.charAt(m-2)=='x' && str.charAt(m)=='z') return true;
    if(str.charAt(m)=='y' && str.charAt(m-1)=='x' && str.charAt(m+1)=='z') return true;
    else return false;
    }

    ReplyDelete
  2. public boolean xyzMiddle(String str) {

    String str2="", str3="";

    for (int i=0; i<=str.length()-3; i++) {

    while (str.substring(i, i+3).equals("xyz")) {

    str2 = str.substring(0, i);
    str3 = str.substring(i+3);

    if (Math.abs(str2.length()-str3.length())<=1)

    return true;

    break;

    }

    }
    return false;
    }

    ReplyDelete
  3. public boolean xyzMiddle(String str) {
    int mid = str.length()/2;

    if(!str.contains("xyz")){
    return false;
    }else if (str.length() % 2 == 1){
    return str.substring(mid-1, mid+2).equals("xyz");
    }
    else {
    return str.substring(mid-1, mid+2).equals("xyz") ||
    str.substring(mid-2, mid+1).equals("xyz");
    }
    }

    ReplyDelete
  4. public boolean xyzMiddle(String str) {
    for (int i = 0; i<=str.length()-3; i++)
    if (str.substring(i,i+3).equals("xyz") &&
    Math.abs(str.length()-i-3-i)<=1) return true;
    return false;
    }

    ReplyDelete
  5. public boolean xyzMiddle(String str) {

    if(str.length() < 3){
    return false;
    }
    if(str.equals("xyz")){
    return true;
    }

    int middle = str.length()/2;
    String test1 = str.substring(middle-1,middle+2);
    String test2 = str.substring(middle-2,middle+1);
    if(str.length()%2 != 0){
    if(test2.matches("\\.{0,1}xyz\\.{0,1}")){
    return false;
    }
    }
    if(test1.matches("\\.{0,1}xyz\\.{0,1}")){
    return true;
    }
    if(test2.matches("\\.{0,1}xyz\\.{0,1}")){
    return true;
    }
    return false;
    }

    ReplyDelete
  6. class XYZtheredemo
    {
    public static void main(String[] args)
    {
    System.out.println(xyzMiddle("AxyzBB"));
    }
    public static boolean xyzMiddle(String str) {
    int len=str.length();
    int z=len-1;

    for(int i=0;i<str.length()-2;i++)
    {
    if(str.substring(i,i+3).equals("xyz"))
    {
    int x=i;
    int y=z-(i+2);

    if(x==y||x-y==1||y-x==1)
    {
    return true;
    }
    }
    }
    return false;
    }
    }

    ReplyDelete
  7. public boolean xyzMiddle(String str) {
    int indexS = 0;
    int indexF = 0;

    if(str.equals("xyz"))
    return true;

    for(int i = 0 ; i < str.length()-3 ; i++){
    if(str.substring(i,i+3).equals("xyz")){
    indexS = i;
    indexF = str.substring(i+3).length();

    if(Math.abs(indexS-indexF) < 2)
    return true;
    }
    }

    return false;
    }

    ReplyDelete
  8. public boolean xyzMiddle(String str) {
    int left = 0;
    int right = 0;
    int diffValue = Integer.MAX_VALUE;
    for(int i = 0; i < str.length()-2; i++) {
    if(str.substring(i).startsWith("xyz")){
    left = str.substring(0,i).length();
    right = str.substring(i+3).length();
    if(Math.abs(left - right) <= 1){
    diffValue = 1;
    }
    }
    }
    return diffValue <= 1 ? true : false;
    }

    ReplyDelete
  9. if(str.length()<3)return false;
    return str.substring(str.length()/2-1, str.length()/2+2).equalsIgnoreCase("xyz") ?
    true : (str.length()%2==0)&&(str.substring(str.length()/2-2, str.length()/2+1).equalsIgnoreCase("xyz"));

    ReplyDelete
  10. Very difficult, but it works

    public boolean xyzMiddle(String str) {

    if (str.length() < 3){
    return false;
    }
    int middle = str.length()/2;

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

    if (str.charAt(middle-1) == 'y' && str.charAt(middle-2) == 'x'
    && str.charAt(middle) == 'z' && first.length()+1 < second.length()){
    return false;
    }

    if (str.charAt(middle-1) == 'y' && str.charAt(middle-2) == 'x'
    && str.charAt(middle) == 'z')
    return true;

    if (str.charAt(middle) == 'y' && str.charAt(middle-1) == 'x'
    && str.charAt(middle+1) == 'z')
    return true;

    else return false;
    }

    ReplyDelete
  11. Took me several tries and complicated looping but while loop works just fine



    public boolean xyzMiddle(String str) {

    while (str.length() > 4 )
    {
    str = str.substring(1, str.length() - 1 );
    }

    return(str.contains("xyz"));

    }

    ReplyDelete
  12. public boolean xyzMiddle(String str) {
    for (int i = 0; i < str.length() - 2; i++)
    {
    if (str.substring(i, i + 3).equals("xyz") && Math.abs(str.length() - (i + 3) - i) <= 1)
    {
    return true;
    }
    }
    return false;
    }

    ReplyDelete
  13. public boolean xyzMiddle(String str) {

    if(!str.contains("xyz")){
    return false;
    }
    if (str.length() % 2 == 1){
    return str.charAt(str.length() / 2 - 1) == 'x'
    && str.charAt(str.length() / 2) == 'y'
    && str.charAt(str.length() / 2 + 1) == 'z';
    }
    return str.charAt(str.length() / 2 - 1) == 'x'
    && str.charAt(str.length() / 2) == 'y'
    && str.charAt(str.length() / 2 + 1) == 'z'
    || str.charAt(str.length() / 2 - 2) == 'x'
    && str.charAt(str.length() / 2 - 1) == 'y'
    && str.charAt(str.length() / 2) == 'z';

    }

    ReplyDelete
  14. public boolean xyzMiddle(String str) {
    final int mid = str.length() / 2;
    if(!str.contains("xyz")){
    return false;
    }
    if (str.length() % 2 == 1){
    return str.charAt(mid - 1) == 'x'
    && str.charAt(mid) == 'y'
    && str.charAt(mid + 1) == 'z';
    }
    return str.charAt(mid - 1) == 'x'
    && str.charAt(mid) == 'y'
    && str.charAt(mid + 1) == 'z'
    || str.charAt(mid - 2) == 'x'
    && str.charAt(mid - 1) == 'y'
    && str.charAt(mid) == 'z';

    }

    ReplyDelete
  15. public boolean xyzMiddle(String str) {

    for (int i=0; i<str.length()-2; i++) {
    if(str.substring(i, i+3).equals("xyz") &&
    Math.abs(i-(str.length()-i-3))<=1) {
    return true;
    }

    }return false;
    }

    ReplyDelete
  16. public boolean xyzMiddle(String str) {
    int countLeft = 0;
    int countRight = 0;
    int size = str.length();
    int half = str.length()/2;
    String tar = "xyz";
    String acc = "";

    if(size < 3){
    return false;
    }
    if(size == 3){
    if(str.equals(tar)){
    return true;
    }
    else{
    return false;
    }
    }
    // Check if xyz near midpoint
    for(int i = half - 2; i < size - 2; i++){
    acc = "";
    // Traverse 3 chars at a time, compare to "xyz" each step
    acc = acc + str.charAt(i) + str.charAt(i + 1) + str.charAt(i + 2);
    if(acc.equals(tar)){
    // Capture index where char z of xyz is found
    int z = i + 2;
    // Store count of remaining letters after xyz
    countRight = size - 1 - z;
    break;
    }
    else{
    countLeft++;
    }
    }
    // Count whatever is left over, which is left of xyz chars count
    countLeft = size - countRight - 3;
    int diff = countLeft - countRight;
    // # of chars left and right of the "xyz" must differ by at most one
    if(diff == 1 || diff == 0 || diff == -1){
    return true;
    }
    return false;
    }

    ReplyDelete
  17. public boolean xyzMiddle(String str) {
    if(str.length()<3) return false;
    int m = str.length()/2;
    if(str.charAt(m-1)=='y' && str.charAt(m-2)=='x' && str.charAt(m)=='z') return true;
    if(str.charAt(m)=='y' && str.charAt(m-1)=='x' && str.charAt(m+1)=='z') return true;
    else return false;
    }

    ReplyDelete
  18. public static boolean xyzMiddle(String str) {
    for(int i=0; i<str.length()-2; i++) {

    if(str.substring(i,i+3).equals("XYZ")) {
    int a=str.substring(0,i).length();
    int b=str.substring(i+3).length();
    if(Math.abs(a-b)<=1) {
    return true;
    }
    }
    }

    return false;
    }

    ReplyDelete

Follow Me

If you like our content, feel free to follow me to stay updated.

Subscribe

Enter your email address:

We hate spam as much as you do.

Upload Material

Got an exam, project, tutorial video, exercise, solutions, unsolved problem, question, solution manual? We are open to any coding material. Why not upload?

Upload

Copyright © 2012 - 2014 Java Problems  --  About  --  Attribution  --  Privacy Policy  --  Terms of Use  --  Contact