A palindrome means that the sequence obtained by traversing a string from left to right and from right to left is the same. That is to say, whether it is read from the left or from the right, it is the same.

For example, "abcba" and "aaa" are palindrome strings, but "abca" is not a palindrome, but a new string "aca" or "aba" obtained by removing a b or c from "abca" is a palindrome string.

Implementation ideas:

  • The head and tail bidirectional loop (double pointer) method is adopted. When the characters are different, there are two cases.

    • Delete one character on the left (equivalent to moving the left pointer one bit to the right), and then compare whether the substring formed by the right pointer is a palindrome string;
    • Delete a character on the right (equivalent to moving the right pointer to the left by one position), and then compare whether the substring formed by the left pointer is a palindrome string;

Example code 1 - while loop:

 private static boolean isPalindrome(String str) {
        if (str == null) {
            return false;
        }
        
        int left = 0;
        int right = str.length() - 1;
        while (left < right) {
            if (str.charAt(left) != str.charAt(right)) {
                return isPalindrome(str, left + 1, right) || isPalindrome(str, left, right - 1);
            }
            left++;
            right--;
        }

        return true;
    }

    /**
     * 比较子串是否为回文字符串
     * @param str
     * @param left
     * @param right
     * @return
     */
    private static boolean isPalindrome(String str, int left, int right) {
        if (str == null) {
            return false;
        }
        
        while (left < right) {
            if (str.charAt(left) != str.charAt(right)) {
                return false;
            }
            left++;
            right--;
        }

        return true;
    }

Because only one character can be deleted at most, when the first different character is encountered, it can be compared at most twice. The above implementation is implemented by using the while loop, or by using the for method.

Example code 2 - for loop

 private static boolean isPalindrome(String str) {
        if (str == null) {
            return false;
        }

        char[] chars = str.toCharArray();
        for (int left = 0, right = chars.length - 1; left < right; left++, right--) {
            if (chars[left] != chars[right]) {
                return isPalindrome(chars, left + 1, right) || isPalindrome(chars, left, right - 1);
            }
        }

        return true;
    }

    private static boolean isPalindrome(char[] chars, int left, int right) {
        for (; left < right; left++, right--) {
            if (chars[left] != chars[right]) {
                return false;
            }
        }

        return true;
    }

Test verification

 public static void main(String[] args) {
        System.out.println(isPalindrome("abcd"));
        System.out.println(isPalindrome("abca"));
        System.out.println(isPalindrome("abcba"));
        System.out.println(isPalindrome("abcda"));
    }

The output is as follows:

false
true
true
false

For more knowledge points related to Java interviews, you can pay attention to the [Java Interview Manual] applet, involving Java foundation, multithreading, JVM, Spring, Spring Boot, Spring Cloud, Mybatis, Redis, database, data structure and algorithm, etc.
Java面试手册


十方
234 声望433 粉丝