Tuesday, April 7, 2020

[Java][Exerise] Valid Palindrome

Method 1 - work method
class Solution {
    public boolean isPalindrome(String s) {
        if(s.isEmpty()) return true;
        String cleanStr = s.toLowerCase().replaceAll("[^a-z0-9]","");
        int end = cleanStr.length()-1;
        for(int start=0; start<(cleanStr.length()/2);start++){
            if(cleanStr.charAt(start)!=cleanStr.charAt(end))
                return false;
            end--;
        }
        return true;
    }
}

Method 2 - cleanest method
class Solution {
    public boolean isPalindrome(String s) {
        if(s.length() == 0) return true;
        StringBuilder sb = new StringBuilder(s.toLowerCase().replaceAll("[^a-z0-9]", ""));             
        return sb.toString().equals(sb.reverse().toString());
    }
}


method 3 - work method with replaceAll
class Solution {
    public boolean isPalindrome(String s) {
        if(s.isEmpty()) return true;
        String cleanStr = s.toLowerCase().replaceAll("[^a-z0-9]","");
        int start = 0;
        int end   = cleanStr.length()-1;
       
        while(start<end){
            if(cleanStr.charAt(start)!=cleanStr.charAt(end)){
                return false;
            }
            end--;
            start++;
        }
        return true;
    }
}

Method 4 - fast method without replaceAll
class Solution {
    public boolean isPalindrome(String s) {
        if(s.isEmpty()) return true;
        int start = 0;
        int end   = s.length()-1;
       
        while(start<end){
            char startChar = Character.toLowerCase(s.charAt(start));
            char endChar = Character.toLowerCase(s.charAt(end));
           
            if(!Character.isLetterOrDigit(startChar)){
                start++;
                continue;
            }
            if(!Character.isLetterOrDigit(endChar)){
                end--;
                continue;
            }
         if(startChar!=endChar){
                return false;
            }
            end--;
            start++;
        }
        return true;
    }
}


Method 5 - fast method checking with ascii code

From this table we can found the range of 0-9 us 48-57, and a-z are 97-122.
class Solution {
    public boolean isPalindrome(String s) {
        if(s.isEmpty()) return true;
        int start = 0;
        int end   = s.length()-1;
       
        while(start<end){
            int startChar = (int) Character.toLowerCase(s.charAt(start));
            int endChar   = (int) Character.toLowerCase(s.charAt(end));
           
            if(!((startChar>=48 && startChar<=57)||(startChar>=97 && startChar<=122))){
                start++;
                continue;
            }
            if(!((endChar>=48 && endChar<=57)||(endChar>=97 && endChar<=122))){
                end--;
                continue;
            }
         if(startChar!=endChar){
                return false;
            }
            end--;
            start++;
        }
        return true;
    }
}

No comments :

Post a Comment