|
| 1 | +""" |
| 2 | +# Intuition |
| 3 | +์๋ฌธ์ ๋ณํ, ์์ซ์ ์ ์ธํ ๋ฌธ์ ์ ๊ฑฐ |
| 4 | +reverse์ ๋น๊ต |
| 5 | +
|
| 6 | +# Approach |
| 7 | +1. non-alphanumeric(isalpha/isnum or ์ ๊ท์) ์ผ ๋ |
| 8 | +2. lower() |
| 9 | +3. ์ ๊ฐ์ฒด๋ฅผ ๋น๊ต |
| 10 | +
|
| 11 | +- chars = "" ์ ๊ฐ์ด |
| 12 | +๋น ๋ฌธ์์ด์ ๋ง๋ค๊ณ ๋ฐ๋ณต๋ฌธ์ ๋๋ฉฐ chars += char ํํ๋ก ๋ฌธ์๋ฅผ ํ๋์ฉ ์ถ๊ฐํ๋ฉด, ๋งค๋ฒ ์๋ก์ด ๋ฌธ์์ด ๊ฐ์ฒด๋ฅผ ์์ฑํ๊ธฐ ๋๋ฌธ์ ์ฑ๋ฅ ๋น์ฉ์ด ํผ. |
| 13 | + -> ๋ฆฌ์คํธ์ ํ๋์ฉ ์ถ๊ฐํ ๋ค, ๋ง์ง๋ง์ "".join() ๋ฉ์๋ ์ฌ์ฉ |
| 14 | +
|
| 15 | +# Complexity |
| 16 | +- Time complexity : O(N) |
| 17 | +- Space complexity : O(N) |
| 18 | +""" |
| 19 | + |
| 20 | + |
| 21 | +class Solution: |
| 22 | + def isPalindrome(self, s: str) -> bool: |
| 23 | + |
| 24 | + chars = [] |
| 25 | + |
| 26 | + for char in s: |
| 27 | + if char.isalnum(): |
| 28 | + chars.append(char.lower()) |
| 29 | + |
| 30 | + result = "".join(chars) |
| 31 | + |
| 32 | + # list comprehension |
| 33 | + # chars = "".join([char.lower() for char in s if char.isalnum()]) |
| 34 | + |
| 35 | + return result == result[::-1] |
| 36 | + |
| 37 | + |
| 38 | +sol = Solution() |
| 39 | +s = "A man, a plan, a canal: Panama" |
| 40 | + |
| 41 | +print(sol.isPalindrome(s)) |
| 42 | + |
| 43 | + |
| 44 | +# Pointer : O(1) |
| 45 | +class Solution: |
| 46 | + def isPalindrome(self, s: str) -> bool: |
| 47 | + |
| 48 | + left, right = 0, len(s) - 1 |
| 49 | + |
| 50 | + while left < right: |
| 51 | + |
| 52 | + if not s[left].isalnum(): |
| 53 | + left += 1 |
| 54 | + continue |
| 55 | + if not s[right].isalnum(): |
| 56 | + right -= 1 |
| 57 | + continue |
| 58 | + |
| 59 | + # while left < right and not s[left].isalnum(): |
| 60 | + # left += 1 |
| 61 | + # while left < right and not s[right].isalnum(): |
| 62 | + # left -= 1 |
| 63 | + |
| 64 | + if s[left].lower() != s[right].lower(): |
| 65 | + return False |
| 66 | + |
| 67 | + left += 1 |
| 68 | + right -= 1 |
| 69 | + |
| 70 | + return True |
| 71 | + |
| 72 | + |
| 73 | +""" ์ ์ฒ๋ฆฌ ํ์ํจ |
| 74 | +class Solution: |
| 75 | + def isPalindrome(self, s: str) -> bool: |
| 76 | +
|
| 77 | + chars = re.sub(r'[^a-zA-Z0-9]', '', s).lower() |
| 78 | +
|
| 79 | + for i in range(len(chars)//2): |
| 80 | + left = chars[i] |
| 81 | + right = chars[len(chars) - 1 - i] |
| 82 | +
|
| 83 | + if left != right: |
| 84 | + return False |
| 85 | + |
| 86 | + return True |
| 87 | +""" |
| 88 | + |
| 89 | +""" ์ ๊ท์ |
| 90 | +import re |
| 91 | +
|
| 92 | +class Solution: |
| 93 | + def isPalindrome(self, s: str) -> bool: |
| 94 | +
|
| 95 | + chars = re.sub(r'[^a-zA-Z0-9]', '', s).lower() |
| 96 | +
|
| 97 | + return chars == chars[::-1] |
| 98 | +""" |
| 99 | + |
| 100 | +""" |
| 101 | +# non-alphanumeric |
| 102 | +- isalpha() |
| 103 | + - ๋ฌธ์์ด์ด ๋ชจ๋ ์ํ๋ฒณ(์๋ฌธ)์ผ๋ก๋ง ๊ตฌ์ฑ๋์ด ์๋์ง ํ์ธ |
| 104 | + - ํ๊ธ, ์ผ๋ณธ์ด ๋ฑ ๋ค๋ฅธ ์ธ์ด์ ๋ฌธ์๋ ํฌํจ ('์๋
ํ์ธ์'.isalpha() โ True) |
| 105 | + - ๊ณต๋ฐฑ, ์ซ์, ํน์๋ฌธ์ ํฌํจ X |
| 106 | +- isnumeric() |
| 107 | + - ๋ฌธ์์ด์ด ๋ชจ๋ ์ซ์๋ก๋ง ๊ตฌ์ฑ๋์ด ์๋์ง ํ์ธ |
| 108 | + - ์ซ์๋ ๋จ์ํ ์๋ผ๋น์ ์ซ์(0-9)๋ฟ๋ง ์๋๋ผ, ์ ๋์ฝ๋ ์ซ์(์: โ , ยฒ, ํ์ ์ซ์)๋ ํฌํจ ('โ โกโข'.isnumeric() โ True) |
| 109 | + - ์์์ (.)์ด๋ ์์ ๋ถํธ(-)๋ ์ซ์๋ก ๊ฐ์ฃผํ์ง X |
| 110 | +- isalnum() |
| 111 | + - isalpha()์ isnumeric()์ ์กฐ๊ฑด์ ํฉ์น ๊ฒ์ผ๋ก, ๋ฌธ์์ด์ด ๋ชจ๋ ์ํ๋ฒณ ๋๋ ์ซ์๋ก ๊ตฌ์ฑ๋์ด ์๋์ง ํ์ธ |
| 112 | +
|
| 113 | +# ๊ฐ์ฒด ์ญ์ |
| 114 | +- .reverse() |
| 115 | + - list ๊ฐ์ฒด์๋ง ์ฌ์ฉ |
| 116 | + - ์๋ณธ ์์ , ๋ฐํ๊ฐ None |
| 117 | +- reversed() |
| 118 | + - ๋ฆฌ์คํธ, ํํ, ๋ฌธ์์ด ๋ฑ ๋ชจ๋ iterable ๊ฐ์ฒด์ ์ฌ์ฉ ๊ฐ๋ฅ |
| 119 | + - ์๋ก์ด ์ญ์ 'iterator' ๊ฐ์ฒด๋ฅผ ๋ฐํ, ์๋ณธ ๊ฐ์ฒด ๋ณ๊ฒฝ X |
| 120 | + - ๋ฐํ๋ ์ดํฐ๋ ์ดํฐ๋ ๋ฆฌ์คํธ๋ ํํ ๋ฑ์ผ๋ก ๋ณํํ์ฌ ์ฌ์ฉํด์ผ ํจ. ๋ฉ๋ชจ๋ฆฌ ํจ์จ์ด ์ค์ํ ๊ฒฝ์ฐ ์ ์ฉ |
| 121 | +- ์ฌ๋ผ์ด์ฑ [::-1] |
| 122 | + - ๋ฆฌ์คํธ, ํํ, ๋ฌธ์์ด ๋ฑ ๋ชจ๋ iterable ๊ฐ์ฒด์ ์ฌ์ฉ ๊ฐ๋ฅ |
| 123 | + - ์๋ณธ์ ๋ณต์ฌํ์ฌ ์ญ์์ผ๋ก ์ ๋ ฌ๋ ์๋ก์ด ๊ฐ์ฒด๋ฅผ ๋ฐํ, ์๋ณธ ๊ฐ์ฒด ๋ณ๊ฒฝ X |
| 124 | + - ์๋ณธ ์ ์ฒด๋ฅผ ๋ณต์ฌํ๋ฏ๋ก ๋ฉ๋ชจ๋ฆฌ ์ฌ์ฉ๋์ด reversed() ๋ณด๋ค ๋ ๋ง์ ์ ์๋ค |
| 125 | +
|
| 126 | +* iterator |
| 127 | +๋ฐ์ดํฐ์ ์์ฐจ์ ์ธ ์ ๊ทผ ๋ฐฉ์์ ์ ์ํ๋ ๊ฐ์ฒด์ผ ๋ฟ, ๊ทธ ์์ฒด๋ก ๋ชจ๋ ๋ฐ์ดํฐ๋ฅผ ๋ฉ๋ชจ๋ฆฌ์ ๋ด๊ณ ์๋ ๋ฆฌ์คํธ๋ ํํ๊ณผ ๊ฐ์ ์ง์ ์ ์ธ ๋ฐ์ดํฐ ๊ตฌ์กฐ๊ฐ ์๋๋ค. |
| 128 | +""" |
0 commit comments