File tree Expand file tree Collapse file tree 1 file changed +37
-0
lines changed Expand file tree Collapse file tree 1 file changed +37
-0
lines changed Original file line number Diff line number Diff line change
1
+ """
2
+ [Problem]
3
+ https://leetcode.com/problems/valid-palindrome/description/
4
+
5
+ ๋ชจ๋ ๋๋ฌธ์๋ฅผ ์๋ฌธ์๋ก ๋ณํํ๊ณ ์ํ๋ฒณ๊ณผ ์ซ์๊ฐ ์๋ ๋ฌธ์๋ค์ ์ ๋ถ ์ ๊ฑฐํํ ์ดํ์ ์์์ ๋ถํฐ ์ผ์ผ๋ ๋ค์์ ๋ถํฐ ์ฝ๋ ๋์ผํ๊ฒ ์ฝํ๋ค๋ฉด, ๊ทธ ๋ฌธ์ฅ์ ํ๋ฌธ์
๋๋ค.
6
+ ์์ซ์ ๋ฌธ์๋ค์ ์ํ๋ฒณ๊ณผ ์ซ์๋ค์ ํฌํจํฉ๋๋ค.
7
+
8
+ [Brainstorming]
9
+ leftPosition๊ณผ rightPosition์ ๋๊ณ , ๋น๊ตํ๋ฉด์ ์๋ ๊ฒฝ์ฐ false๋ฅผ returnํ๋ค. => O(s.length)
10
+
11
+ [Complexity]
12
+ N: s.length
13
+ Time: O(1/2 * N) => O(N)
14
+ Space: O(1)
15
+ """
16
+
17
+ class Solution :
18
+ def isPalindrome (self , s :str )-> bool :
19
+ leftPos , rightPos = 0 , len (s ) - 1
20
+ while leftPos < rightPos :
21
+ while not s [leftPos ].isalnum () and leftPos < rightPos :
22
+ leftPos += 1
23
+ while not s [rightPos ].isalnum () and leftPos < rightPos :
24
+ rightPos -= 1
25
+
26
+ if s [leftPos ].upper () != s [rightPos ].upper ():
27
+ return False
28
+ leftPos += 1
29
+ rightPos -= 1
30
+ return True
31
+
32
+ sol = Solution ()
33
+ print (sol .isPalindrome ("A man, a plan, a canal: Panama" ) == True )
34
+ print (sol .isPalindrome ("race a car" ) == False )
35
+ print (sol .isPalindrome (" " ) == True )
36
+ print (sol .isPalindrome ("0P" ) == False )
37
+ print (sol .isPalindrome ("a" ) == True )
You canโt perform that action at this time.
0 commit comments