Skip to content

Commit d3ef18f

Browse files
committed
valid palindrome solution(py, ts)
1 parent b7d76ef commit d3ef18f

File tree

2 files changed

+41
-0
lines changed

2 files changed

+41
-0
lines changed

valid-palindrome/hi-rachel.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
# O(n) time, O(1) space
2+
# isalnum() -> 문자열이 영어, 한글 혹은 숫자로 되어있으면 참 리턴, 아니면 거짓 리턴.
3+
4+
class Solution:
5+
def isPalindrome(self, s: str) -> bool:
6+
l = 0
7+
r = len(s) - 1
8+
9+
while l < r:
10+
if not s[l].isalnum():
11+
l += 1
12+
elif not s[r].isalnum():
13+
r -= 1
14+
elif s[l].lower() == s[r].lower():
15+
l += 1
16+
r -= 1
17+
else:
18+
return False
19+
return True
20+

valid-palindrome/hi-rachel.ts

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
// O(n) time, O(1) space
2+
3+
function isPalindrome(s: string): boolean {
4+
let low = 0,
5+
high = s.length - 1;
6+
7+
while (low < high) {
8+
while (low < high && !s[low].match(/[0-9a-zA-Z]/)) {
9+
low++;
10+
}
11+
while (low < high && !s[high].match(/[0-9a-zA-Z]/)) {
12+
high--;
13+
}
14+
if (s[low].toLowerCase() !== s[high].toLowerCase()) {
15+
return false;
16+
}
17+
low++;
18+
high--;
19+
}
20+
return true;
21+
}

0 commit comments

Comments
 (0)