Accepted O(n) Solution


  • 0
    J
        public boolean isPalindrome(String s) {
            if(s.length() == 0)
                return true;
            s = s.replaceAll("[^a-zA-Z0-9]", "");
            s = s.toLowerCase();
            
            char[] reverse = new char[s.length()];
            int j = s.length()-1;
            for(int i = 0; i < s.length(); i++){
                reverse[j] = s.charAt(i);
                j--;
            }
            for(int i = 0; i < s.length(); i++){
                if(reverse[i] == s.charAt(i))
                    continue;
                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.