Skip to content

Commit bc10660

Browse files
committed
Create: 0125-valid-palindrome.dart
1 parent 2ddf982 commit bc10660

File tree

1 file changed

+32
-0
lines changed

1 file changed

+32
-0
lines changed

Diff for: dart/0125-valid-palindrome.dart

+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
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+
}

0 commit comments

Comments
 (0)