Java – given a string, does “XYZ” appear in the middle of the string?

My solution works without the second line, except for one condition: if STR = "xYx" is it possible to modify the for loop to take this into account... I'm trying to understand why it doesn't

My solution really works. I just want to better understand what I'm doing I know I can add it to the first if statement, but I wonder why it doesn't work without it

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

Solution

I think I remember this question - I believe it's this question from codingbat Excellent website. When I started programming, I learned a lot from the website However, there is absolutely no reason to use loops

public boolean xyzMiddle(String str) {
  boolean result = false; 
  int i = str.length()/2 -1;

  if (str.length() >= 3 && (str.substring(i,i+3).equals("xyz") || (str.length()%2 == 0 && str.substring(i-1,i+2).equals("xyz"))  )) {
      result = true;
  }
  return result;
}

So let's look at this and why it works First, str.length() > = 3, because a string cannot contain "XYZ" if it is not at least as long as "XYZ"

There are two main cases of this problem that we need to consider Strings can have uniform or uneven lengths In case of imbalance, it is easy to:

Unbalanced situation

AAAxyzAAA // length = 9
012345678 // the indexes
    ^     // that's the middle,which can be calculated by length/2
          // (since this is an integer divison,we disregard whatever comes after the decimal point)

So, to get the beginning of XYZ substring, we just need to subtract one from this number - that's what I'm doing:

AAAxyzAAA // length = 9
012345678 // the indexes
   i      // i = length/2-1 = 3

So if str.substring (I, I, 3) is XYZ, we can return true!

Even cases now, this can be a bit tricky because the string has no real "middle" In fact, the two indexes can be called the middle, so we have two sub cases:

AAAAAAAA // length = 8
01234567 // the indexes
   ^^    // Which one is the true middle character?

In fact, the middle is between indexes 3 and 4 However, when we perform integer division, length / 2 is always the largest (rightmost) of the two possible "middle" Because we use the intermediate calculation I, the application of the same - str.substring (I, I, 3) in the case of non-uniformity can be regarded as the middle part of the string

AAAxyzAA 
01234567 
   ^^^     // str.substring(i,i+3)
   i

But suppose our string is aaxyzaaa - it can also be considered the middle part of the string So we need to check the substring for "move left" – so we subtract 1. 0 from it

AAxyzAAA 
01234567 
  ^^^      // str.substring(i-1,i+2)
   i       // i is still at "the old" location

Is it even or not?

To check whether the string is even or uneven, we use the modulus operator% The easiest way to think about its role is "what will be left after dividing with this number?" So 3% 2 will be 1 In our example, we want to make sure that the number can be divided by 2 without leaving anything - because that means it's even Therefore, before performing the "move left" check, we need to check whether str.length()% 2 = = 0 is correct If not, we may risk going beyond the bounds of the string If the string is 3 characters long, we move one to the left... We will check the substring starting from index - 1, which doesn't make much sense

Put them together and you go!

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>