Quick and easy to understand Java solution O(n)


  • 0
    K
     public boolean isPalindrome(String s) {
            /* Replace all punctuation, remove white spaces and 
             * make everything lowercase.
             */
            String justLetters = s.replaceAll("[^a-zA-Z0-9]", "");
            justLetters = justLetters.replaceAll("\\s+", "").toLowerCase();
            
           /*  Read the string from front to back and from back to front. 
            *  Check if the characters are matching.
            */
            int j = justLetters.length() - 1;
            for(int i = 0; i < justLetters.length(); i++) {
                if(justLetters.charAt(i) != justLetters.charAt(j)) {
                    return false;
                }
                j--;
            }
            
            return true;
        }
    

Log in to reply
 

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