Accepted Straightforward JAVA solution, Cost 5ms, Beat 97.00%


  • 0
    J
    public class Solution {
        public boolean isPalindrome(String s) {
            if (s == null || s.length() == 0 || s.trim().length() == 0) return true;
            int len = s.length(), lo = 0, hi = len - 1, tag1 = 0, tag2 = 0;
            if (len == 1) return true;
            char[] ch = s.toCharArray();
            while (lo <= hi) {
                if ((ch[lo] >= 'a' && ch[lo] <= 'z') || (ch[lo] >= 'A' && ch[lo] <= 'Z')) {
                    
                } else if ((ch[lo] - '0' >= 0 && ch[lo] - '0' <= 9)) {
                    tag1 = 1;
                } else {
                    lo++;
                    continue;
                }
                
                if ((ch[hi] >= 'a' && ch[hi] <= 'z') || (ch[hi] >= 'A' && ch[hi] <= 'Z')) {
                    
                } else if (ch[hi] - '0' >= 0 && ch[hi] - '0' <= 9) {
                    tag2 = 1;
                } else {
                    hi--;
                    continue;
                }
                
                if (tag1 == 0 && tag2 == 0) {
                if (ch[lo] == ch [hi] || ch[lo] == ch[hi] - 32 || ch[lo] == ch[hi] + 32) {
                    lo++;
                    hi--;
                } else return false;
                } else if (tag1 == 1 && tag2 == 1) {
                    if (ch[lo] == ch[hi]) {
                        lo++;
                        hi--;
                    } else {
                        return false;
                    }
                } else return false;
                
            }
            
            return true;
        }
    }

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.