Skip to content

Latest commit

 

History

History
33 lines (27 loc) · 1022 Bytes

125. Valid Palindrome.MD

File metadata and controls

33 lines (27 loc) · 1022 Bytes

Given a string, determine if it is a palindrome, considering only alphanumeric characters and ignoring cases.

For example,
"A man, a plan, a canal: Panama" is a palindrome.
"race a car" is not a palindrome.

Note: Have you consider that the string might be empty? This is a good question to ask during an interview.

For the purpose of this problem, we define empty string as valid palindrome.

    public boolean isPalindrome(String s) {
        int len = s.length();
        StringBuffer sb = new StringBuffer();
        StringBuffer sb2 = new StringBuffer();
        for(int i = 0; i < len ; i++){
        	char m = s.charAt(i);
        if( (m >= '0' && m <= '9') || (m >= 'A' && m <= 'Z' )|| (m >= 'a' && m <= 'z' )){
        		sb = sb.append(m);
        	}
        }
        for(int i = sb.length()-1; i >=0; i--){
        	char m = sb.charAt(i);        	
        	sb2 = sb2.append(m);       	
        }

    	return  sb.toString().toLowerCase().endsWith(sb2.toString().toLowerCase());   
    }