Skip to content

Commit 6151023

Browse files
committed
Add solution for Valid Palindrome problem
1 parent 307356c commit 6151023

1 file changed

Lines changed: 27 additions & 0 deletions

File tree

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
"""
2+
# Approach
3+
์–‘ ๋์— ํฌ์ธํ„ฐ๋ฅผ ๋‘๊ณ  ํŒŒ์ด์ฌ์˜ isalnum(), lower() ๋ฌธ์ž์—ด ๋ฉ”์†Œ๋“œ๋กœ ๊ฒ€์‚ฌํ•ฉ๋‹ˆ๋‹ค.
4+
5+
# Complexity
6+
- Time complexity: s์˜ ๊ธธ์ด๋ฅผ N์ด๋ผ๊ณ  ํ•  ๋•Œ, O(N)
7+
8+
- Space complexity: O(1)
9+
"""
10+
11+
12+
class Solution:
13+
def isPalindrome(self, s: str) -> bool:
14+
l = 0
15+
r = len(s) - 1
16+
while l < r:
17+
if not s[l].isalnum():
18+
l += 1
19+
continue
20+
if not s[r].isalnum():
21+
r -= 1
22+
continue
23+
if s[l].lower() != s[r].lower():
24+
return False
25+
l += 1
26+
r -= 1
27+
return True

0 commit comments

Comments
ย (0)