Accepted Java Solution(15ms) Easy


  • 2
    F

    All you need to do is parse out the alphanumeric characters using StringBuilder then perform a regular palindrome search.

    public class Solution {
        public boolean isPalindrome(String s) {
            if(s == null || s.isEmpty()) return true;
            //Parsing lowercase alphanumeric chars
            StringBuilder sb = new StringBuilder();
            for(char c: s.toLowerCase().toCharArray()){
                if(Character.isLetter(c) || Character.isDigit(c)) sb.append(c);
            }
            //Performing regular palindrome search
            int i = 0, j = sb.length()-1;
            while(i <= j){
                if(sb.charAt(i++) != sb.charAt(j--)) return false;
            }
            
            return true;
        }
    }
    

Log in to reply
 

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