Test cases
|
|
Solution 1: accepted 10ms
Two pointers.
|
|
Improved to 8ms using Character functions.12345678910111213141516171819202122232425public boolean isPalindrome(String s) { int l = 0; int r = s.length() - 1; while (l < r) { char lc = s.charAt(l); char rc = s.charAt(r); if (!Character.isLetterOrDigit(lc)) { l++; continue; } if (!Character.isLetterOrDigit(rc)) { r--; continue; } if (Character.toLowerCase(lc) != Character.toLowerCase(rc)) { return false; } else { l++; r--; } } if(l < r && s.charAt(l) != s.charAt(r)) // two left in the middle return false; return true;}