File tree 1 file changed +32
-0
lines changed
1 file changed +32
-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
+ static bool isAlphaNum (String s) {
6
+ if ((s.codeUnits[0 ] >= 'a' .codeUnitAt (0 ) &&
7
+ s.codeUnits[0 ] <= 'z' .codeUnitAt (0 )) ||
8
+ (s.codeUnits[0 ] >= 'A' .codeUnitAt (0 ) &&
9
+ s.codeUnits[0 ] <= 'Z' .codeUnitAt (0 )) ||
10
+ (s.codeUnits[0 ] >= '0' .codeUnitAt (0 ) &&
11
+ s.codeUnits[0 ] <= '9' .codeUnitAt (0 ))) {
12
+ return true ;
13
+ }
14
+ return false ;
15
+ }
16
+
17
+ bool isPalindrome (String s) {
18
+ var left = 0 , right = s.length - 1 ;
19
+
20
+ while (left < right) {
21
+ while (! Solution .isAlphaNum (s[left]) && left < right) left++ ;
22
+ while (! Solution .isAlphaNum (s[right]) && left < right) right-- ;
23
+
24
+ if (s[left].toLowerCase () != s[right].toLowerCase ()) return false ;
25
+
26
+ left++ ;
27
+ right-- ;
28
+ }
29
+
30
+ return true ;
31
+ }
32
+ }
You can’t perform that action at this time.
0 commit comments