125. Verify palindrome-Java

Given a string, verify whether it is a palindrome, consider only letters and numbers, and ignore the case of letters.

Explanation: In this question, we define an empty string as a valid palindrome string.

Example 1:
Input: "A man, a plan, a canal: Panama"
Output: true

Example 2:
Input: "race a car"
Output: false

Source: LeetCode
Link: https://leetcode-cn.com/problems/valid-palindrome

Method 1: Screening + Judgment

Keep the letters and numbers in s and place them in another string sgood. In this way, we only need to judge whether sgood is a normal palindrome.

There are two ways to judge. The first is to use the string reversal API in the language to get the reversed string sgood_rev of sgood. As long as the two strings are the same, sgood is a palindrome string.

public static boolean isPalindrome(String s) {
    
    
   StringBuilder sgood = new StringBuilder();
    int length = s.length();
    for (int i = 0; i < length; i++) {
    
    
        char ch = s.charAt(i);
        //判断字符是否为字母或数字
        if (Character.isLetterOrDigit(ch)) {
    
    
            //将ch变为小写
            sgood.append(Character.toLowerCase(ch));
        }
    }
    //将sgood反转
    StringBuffer sgood_rev = new StringBuffer(sgood).reverse();
    return sgood.toString().equals(sgood_rev.toString());
}

The second is to use double pointers.

public static boolean isPalindrome(String s) {
    
    
  StringBuilder sgood = new StringBuilder();
    int length = s.length();
    for (int i = 0; i < length; i++) {
    
    
        char ch = s.charAt(i);
        if (Character.isLetterOrDigit(ch)) {
    
    
            sgood.append(Character.toLowerCase(ch));
        }
    }
    int n = sgood.length();
    int left = 0, right = n - 1;
    while (left < right) {
    
    
        if (Character.toLowerCase(sgood.charAt(left)) != Character.toLowerCase(sgood.charAt(right))) {
    
    
            return false;
        }
        left++;
        right--;
    }
    return true;
}

Method 2: Judging directly on the original string

We can optimize the second method of judging palindrome in Method 1, and we can get an algorithm that only uses O(1) space.

We directly use double pointers on the original string s. When moving any pointer, you need to keep moving in the direction of the other pointer until you encounter a letter or number character, or the two pointers overlap. In other words, each time we move the pointer to the next alphabetic character or numeric character, we judge whether the characters pointed to by the two pointers are the same.

public static boolean isPalindrome(String s) {
    
    
    int i = 0, j = s.length() - 1;
    while (i < s.length()) {
    
    
        if (!Character.isLetterOrDigit(s.charAt(i))) {
    
    
            i++;
            continue;
        }

        if (!Character.isLetterOrDigit(s.charAt(j))) {
    
    
            j--;
            continue;
        }

        if (Character.toLowerCase(s.charAt(i)) != Character.toLowerCase(s.charAt(j))) {
    
    
            return false;

        } else {
    
    
            i++;
            j--;
        }
    }
    return true;
}

Guess you like

Origin blog.csdn.net/m0_46390568/article/details/108281754