Basic Idea is :
1. Remove any non alphanumeric characters from string.
2. Iterate over the array with two pointers, one at end and one at start of the string.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | public class Solution { public boolean isPalindrome(String s) { if(s==null||s.length()==0 || s.length() == 1) return true; s = s.replaceAll("[^a-zA-Z0-9]", "").toLowerCase(); for(int i = 0; i < s.length() ; i++){ if(s.charAt(i) != s.charAt(s.length() - 1 - i)){ return false; } } return true; } } |
No comments:
Post a Comment