File tree Expand file tree Collapse file tree 1 file changed +31
-0
lines changed
Expand file tree Collapse file tree 1 file changed +31
-0
lines changed Original file line number Diff line number Diff line change 1+ ๏ปฟ# --- ํด์ ---
2+ #๋งค๊ฐ๋ณ์ s๋ฅผ ์๋ฌธ์๋ก ๋ณํ ํ non alpha numeric(์ํ๋ฒณ๊ณผ ์ซ์ ์ด์ธ์ ๋ชจ๋ ๊ฒ)์ ์ ๊ฑฐํ๊ณ ๋น์นธ์ replace๋ก ์ ๊ฑฐํ ํ temp์ ์ ์ฅํ๋ค
3+ #temp์ temp๋ฅผ ๋ค์งํ string(temp[::-1])์ ๋น๊ตํ์ฌ, ๋์ด ๊ฐ์ผ๋ฉด palindrome์ด๋ค.
4+
5+ # --- Big O
6+ #N: ๋งค๊ฐ๋ณ์ s์ ๊ธธ์ด๊ฐ N์ด๋ค.
7+
8+ # Time Complexity: O(N)
9+ #- temp๋ s์ ๊ธธ์ด์ ๊ธฐ๋ฐํ์ฌ ์์ฑ๋๋ค: O(N)
10+ #- ๋ฌธ์์ด ๋ค์ง๊ธฐ(temp[::-1])๋ s์ ๊ธธ์ด์ ๊ธฐ๋ฐํ๋ค : O(N)
11+ #- temp == temp[::-1]๋ ๋ ๋ฌธ์์ด์ด ๊ธธ์ด์ ๋ฌธ์ ํ๋ํ๋๊ฐ ๊ฐ์์ง ํ์ธ :O(N)
12+
13+ # Space Complexity: O(N)
14+ #-temp๋ s์ ๊ธธ์ด์ ์ํด ์์ฑ๋๋ฏ๋ก n์ ์ํฅ๋ฐ์(The temp requires extra space depends on the size of s) : O(N)
15+
16+ class Solution (object ):
17+ def isPalindrome (self , s ):
18+ """
19+ :type s: str
20+ :rtype: bool
21+ """
22+
23+ #removes all non alpha numeric(only accept alphabet and number) items from the s
24+ temp = lower (s )
25+ temp = " " .join (re .split ("[^a-zA-Z0-9]*" ,temp )).replace (" " ,"" )
26+ #Compare with temp and reverse temp
27+ #If they are same, it is palindrome
28+ if temp == temp [::- 1 ]:
29+ return True
30+ else :
31+ return False
You canโt perform that action at this time.
0 commit comments