Skip to content

Commit c3733dd

Browse files
committed
solve: valid-palindrome
1 parent abdbd1d commit c3733dd

File tree

1 file changed

+31
-0
lines changed

1 file changed

+31
-0
lines changed
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
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

0 commit comments

Comments
ย (0)