File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change 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
You canโt perform that action at this time.
0 commit comments