Skip to content

Commit 828972b

Browse files
committed
valid palindrome solution commit
1 parent 260b26c commit 828972b

File tree

1 file changed

+24
-0
lines changed

1 file changed

+24
-0
lines changed
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
// ์‹œ๊ฐ„ ๋ณต์žก๋„: O(n)
2+
// ๊ณต๊ฐ„ ๋ณต์žก๋„: O(n)
3+
function isPalindrome(s: string): boolean {
4+
//1. s์—์„œ ๋ฌธ์ž์™€ ์ˆซ์ž๊ฐ€ ์•„๋‹Œ ๋‹ค๋ฅธ ๊ฒƒ๋“ค์€ ์ œ๊ฑฐ
5+
//2. ๋Œ€๋ฌธ์ž -> ์†Œ๋ฌธ์ž, ๋„์–ด์“ฐ๊ธฐ, ๊ณต๋ฐฑ ์ œ๊ฑฐ
6+
let originalString = s;
7+
let alphabeticString = originalString.replace(/[^a-zA-Z0-9]/g, '');
8+
const filteredString = alphabeticString.toLowerCase().trim();
9+
10+
//3. ์ œ๊ฑฐ ํ›„ length === 0 ์ผ ๊ฒฝ์šฐ true๋กœ ๊ฒฐ๊ณผ ๊ฐ’ ๋ฐ˜ํ™˜.
11+
if (filteredString.length === 0) {
12+
return true;
13+
}
14+
15+
let count = 0;
16+
let roundedStringLength = Math.round(filteredString.length / 2);
17+
for (let i = 0; i < roundedStringLength; i++) {
18+
// ์–‘ ๋์˜ ๊ธ€์ž๊ฐ€ ๋™์ผํ•œ์ง€ ์ฒดํฌํ•˜๊ณ 
19+
if (filteredString[i] === filteredString[filteredString.length - i - 1]) {
20+
count += 1;
21+
}
22+
}
23+
return count === roundedStringLength ? true : false;
24+
}

0 commit comments

Comments
ย (0)