File tree Expand file tree Collapse file tree 1 file changed +51
-0
lines changed Expand file tree Collapse file tree 1 file changed +51
-0
lines changed Original file line number Diff line number Diff line change 1+ // time complexity : O(n)
2+ // space complexity : O(1)
3+
4+ class Solution {
5+ public boolean isPalindrome (String s ) {
6+ s = s .toLowerCase ();
7+ int i = 0 ;
8+ int j = s .length () - 1 ;
9+ while (i < j ) {
10+ char c1 = s .charAt (i );
11+ if (!isAlphanumeric (c1 )) {
12+ i ++;
13+ continue ;
14+ }
15+
16+ char c2 = s .charAt (j );
17+ if (!isAlphanumeric (c2 )) {
18+ j --;
19+ continue ;
20+ }
21+
22+ if (c1 != c2 ) {
23+ return false ;
24+ }
25+ i ++;
26+ j --;
27+ }
28+
29+ return true ;
30+ }
31+
32+ private boolean isAlphanumeric (char c ) {
33+ return (c >= '0' && c <='9' ) || (c >= 'A' && c <= 'Z' ) || (c >= 'a' && c <= 'z' );
34+ }
35+ }
36+
37+ /*
38+ class Solution {
39+ public boolean isPalindrome(String s) {
40+ s = s.toLowerCase();
41+ String[] split = s.replaceAll("[^A-Za-z0-9]", "").split("");
42+ for (int i=0; i<split.length/2; i++) {
43+ if (!split[i].equals(split[split.length-1-i])) {
44+ return false;
45+ }
46+ }
47+
48+ return true;
49+ }
50+ }
51+ */
You can’t perform that action at this time.
0 commit comments