Verify the palindrome string

Given a string, verify that it is a palindrome string. Only consider alphanumeric characters. You can ignore the case of the letters.

Note: In this case, we define an empty string as a valid palindrome string.

See the LeetCode website for an example.

Source: LeetCode Link: https://leetcode-cn.com/probl… Copyright belongs to collar network. Commercial reprint please contact the official authorization, non-commercial reprint please indicate the source.

Solution 1: string traversal

The main thing is to use some library functions to iterate over strings.

First, it returns true if s is empty or if s has length 1.

Otherwise, the traversal starts from the first front and last end of s, and the traversal process is as follows:

  • If frontChar is not an alphabetic or numeric character, front moves back one step for the next traversal.
  • If the character endChar corresponding to end is not an alphanumeric character, end moves forward one bit for the next traversal.
  • If frontChar or endChar are alphanumeric characters, first convert them to uppercase characters if frontChar or endChar are alphanumeric characters (because there is no need to be case sensitive), then compare frontChar and endChar for equality, and return false if they are not. If they are equal, front moves back one place and end moves forward one place for the next traversal.
  • The condition for the end of the traversal is that front is not less than end.
public class LeetCode_125 { public static boolean isPalindrome(String s) { if (s == null || s.length() == 1) { return true; } int front = 0, end = s.length() - 1; while (front <= end) { char frontChar = s.charAt(front); char endChar = s.charAt(end); if ((frontChar >= 'a' && frontChar <= 'z') || (frontChar >= 'A' && frontChar <= 'Z') || (frontChar >= '0' && frontChar <= '9')) { if ((endChar >= 'a' && endChar <= 'z') || (endChar >= 'A' && endChar <= 'Z') || (endChar >= '0' && endChar <=  '9')) { if (Character.isAlphabetic(frontChar)) { frontChar = Character.toUpperCase(frontChar); } if (Character.isAlphabetic(endChar)) { endChar = Character.toUpperCase(endChar); } if (frontChar ! = endChar) { return false; } else { front++; end--; } } else { end--; } } else { front++; } } return true; } public static void main(String[] args) { System.out.println(isPalindrome("A man, a plan, a canal: Panama")); }}

【 Daily Message 】
Life is like water without cliffs, clouds blowing for snow, the world taste boiled into tea.