Skip to content

Commit f70016b

Browse files
committed
sadie100: valid palindrome solution
1 parent 7da8e35 commit f70016b

1 file changed

Lines changed: 23 additions & 0 deletions

File tree

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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

Comments
ย (0)