We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent 7da8e35 commit f70016bCopy full SHA for f70016b
1 file changed
โvalid-palindrome/sadie100.tsโ
@@ -0,0 +1,23 @@
1
+/*
2
+s์์ ์ํ๋ฒณ๊ณผ ์ซ์๊ฐ ์๋ ๊ธ์๋ฅผ ๋ค ๋ ๋ฆฌ๊ณ , ์คํ์ ๋ฌธ์๋ฅผ ์ ๋ฐ๊น์ง ๋ด์ ํ ์ ๋ฐ ์ดํ๋ถํฐ๋ ๊บผ๋ด๊ฐ๋ฉฐ ์๋ค ๋ ํฐ๊ฐ ์ผ์นํ๋์ง ํ์ธํ๋ค
3
+
4
+์๊ฐ๋ณต์ก๋ : O(N) (N์ s์ ๊ธธ์ด)
5
+๊ณต๊ฐ๋ณต์ก๋ : O(N) (์คํ)
6
+*/
7
8
+function isPalindrome(s: string): boolean {
9
+ const newStr = s.replace(/[^a-zA-Z0-9]/g, '').toLowerCase()
10
+ const isStrEven = newStr.length % 2 === 0
11
+ const lenHalf = Math.floor(newStr.length / 2)
12
+ const stack = []
13
+ for (let i = 0; i < newStr.length; i++) {
14
+ if (i < lenHalf) {
15
+ stack.push(newStr[i])
16
+ } else {
17
+ if (!isStrEven && i === lenHalf) continue
18
+ const isSame = newStr[i] === stack.pop()
19
+ if (!isSame) return false
20
+ }
21
22
+ return true
23
+}
0 commit comments