当前位置:网站首页>Leetcode advanced road - 125 Validate palindrome string

Leetcode advanced road - 125 Validate palindrome string

2022-06-10 21:22:00 Li_ XiaoJin

 Given a string , Verify that it is a palindrome string , Consider only alphabetic and numeric characters , The case of letters can be ignored .

 explain : 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

Learn a new method for this problem Character.isLetterOrDigit()java.lang.Character.isLetterOrDigit(char ch) Determines whether the specified character is alphabetic or numeric .java.lang.Character.isLetter(char ch) Make sure that the specified character is a letter .

/**
 * 125.  Verify the palindrome string 
 * @Author: lixj
 * @Date: 2020/9/21 11:08
 */
public class ValidPalindrome {
    /**
     *  Splice the new string and then judge , Double finger needling 
     * @param s
     * @return
     */
    public boolean isPalindrome(String s) {
        char[] ch = s.toCharArray();
        int len = s.length();
        if (len == 0) return true;
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i <= len -1; i++) {
            if (Character.isLetterOrDigit(ch[i])) {
                sb.append(ch[i]);
            }
        }

        int left = 0;
        int right = sb.length() -1;
        char[] chnew = sb.toString().toLowerCase().toCharArray();
        while (left < right) {
            if (chnew[left] != chnew[right]) {
                return false;
            }
            left++;
            right--;
        }
        return true;
    }

    /**
     *  The original string judgment 
     * @param s
     * @return
     */
    public boolean isPalindrome1(String s) {
        int left = 0;
        int right = s.length() -1;
        while (left < right) {
            while (left < right && !Character.isLetterOrDigit(s.charAt(left))) {
                ++left;
            }
            while (left < right && !Character.isLetterOrDigit(s.charAt(right))) {
                --right;
            }

            if (left < right) {
                if (Character.toLowerCase(s.charAt(left)) != Character.toLowerCase(s.charAt(right))) {
                    return false;
                }
            }
            ++left;
            --right;
        }
        return true;
    }


    public static void main(String[] args) {
        String srt = "A man, a plan, a canal: Panama";
//        String srt = "A man, a plan, a canal: vnama";
        ValidPalindrome validPalindrome = new ValidPalindrome();
        System.out.println(validPalindrome.isPalindrome1(srt));
    }
}

Copyright: use Creative Commons signature 4.0 International license agreement to license Links:https://lixj.fun/archives/leetcode Advanced road -125 Verify the palindrome string

原网站

版权声明
本文为[Li_ XiaoJin]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/161/202206101959594922.html