Skip to content

Commit db46313

Browse files
authored
feat: add palindrom problem
1 parent ff57c5b commit db46313

1 file changed

Lines changed: 35 additions & 0 deletions

File tree

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
class Solution:
2+
def isPalindrome(self, s: str) -> bool:
3+
"""
4+
string์ด ํŒฐ๋ฆฐ๋“œ๋กฌ ๋ฌธ์ž์—ด์ธ์ง€ ํŒ๋‹จํ•˜๋Š” ํ•จ์ˆ˜
5+
ํŒฐ๋ฆฐ๋“œ๋กฌ: ๋Œ€์†Œ๋ฌธ์žx ์˜๋ฌธ์ž&์ˆซ์ž ๋งŒ์œผ๋กœ ์•ž๋’ค๊ฐ€ ๋™์ผํ•œ ๋ฌธ์ž์—ด
6+
7+
๋ฐฉ๋ฒ•:
8+
1. ๋ฆฌ์ŠคํŠธ๋ฅผ ํ•„ํ„ฐ/์†Œ๋ฌธ์žํ™”ํ•ด์„œ, ์•ž์—์„œ ๋ฐ˜์ ˆ๊ณผ ๋’ค์˜ ๋ฐ˜์ ˆ (reverse)ํ•œ ๊ฒƒ์„ ๋น„๊ตํ•˜๊ธฐ
9+
-> ์ˆœํšŒ๋ฅผ ๋‘๋ฒˆํ•˜๊ฒŒ ๋จ
10+
2. ์•ž๊ณผ ๋’ค์—์„œ ํฌ์ธํ„ฐ ๋ฐฉ์‹์œผ๋กœ ์ฐพ๊ธฐ
11+
-> ์˜๋ฌธ์žํ˜น์€ ์ˆซ์ž๊ฐ€ ์•„๋‹ˆ๋ฉด ๊ฑด๋„ˆ๋›ฐ๊ธฐ
12+
-> isalnum์œผ๋กœ ์œ ํšจํ•œ ๋ฌธ์ž์ธ์ง€ ํŒ๋‹จ
13+
-> ์‹œ๊ฐ„ ๋ณต์žก๋„ O(n) ๊ณต๊ฐ„ ๋ณต์žก๋„ O(1)
14+
Args:
15+
s (str): ๊ฒ€์‚ฌํ•  ๋ฌธ์ž์—ด
16+
17+
Returns:
18+
bool: ํŒฐ๋ฆฐ๋“œ๋กฌ์ด๋ฉด True, ์•„๋‹ˆ๋ฉด False
19+
"""
20+
i, j = 0, len(s)-1
21+
answer = True
22+
while i < len(s) and j >= 0 and i < j:
23+
if s[i].isalnum() == False:
24+
i += 1
25+
elif s[j].isalnum() == False:
26+
j -= 1
27+
else:
28+
if s[i].lower() == s[j].lower():
29+
answer = answer and True
30+
else:
31+
answer = answer and False
32+
break
33+
i += 1
34+
j -= 1
35+
return answer

0 commit comments

Comments
ย (0)