Skip to content

Commit 45d0647

Browse files
committed
1. valid palindrome
1 parent 9ae8457 commit 45d0647

File tree

1 file changed

+34
-0
lines changed

1 file changed

+34
-0
lines changed

โ€Žvalid-palindrome/whewchews.ts

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
/*
2+
* ์กฐ๊ฑด
3+
* ๋ชจ๋“  ๋Œ€๋ฌธ์ž๋ฅผ ์†Œ๋ฌธ์ž๋กœ ๋ฐ”๊ฟ”์„œ ์ฒ˜๋ฆฌ
4+
* ์˜์–ด ๋Œ€์†Œ๋ฌธ์ž, ์ˆซ์ž๊ฐ€ ์•„๋‹Œ ๋ฌธ์ž๋Š” ์ œ๊ฑฐ ํ›„ ์ฒดํฌ
5+
*
6+
* ์•„์ด๋””์–ด
7+
* ์ •๊ทœ์‹์œผ๋กœ ๋ฌธ์ž์—ด ์ค‘์— ์ˆซ์ž, ๋ฌธ์ž๋งŒ ์ถ”์ถœ
8+
* ์ง์ˆ˜์ธ ๊ฒฝ์šฐ 123456
9+
* 1-6, 2-5, 3-4๋ฅผ ๋น„๊ต: length/2๋ฒˆ ๋น„๊ต
10+
* ํ™€์ˆ˜์ธ ๊ฒฝ์šฐ 1234567
11+
* 1-7, 2-6, 3-5๋ฅผ ๋น„๊ต: length/2๋ฒˆ ๋น„๊ต
12+
*/
13+
14+
function isPalindrome(s: string): boolean {
15+
// TC: O(n)
16+
// SC: O(n)
17+
const str = s
18+
.replace(/[^a-zA-Z0-9]/g, "")
19+
.replace(/\s+/g, "")
20+
.toLocaleLowerCase();
21+
const len = str.length;
22+
23+
// TC: O(n)
24+
for (let i = 0; i <= str.length / 2 - 1; i++) {
25+
if (str[i] !== str[len - i - 1]) {
26+
return false;
27+
}
28+
}
29+
30+
return true;
31+
}
32+
33+
// TC: O(n)
34+
// SC: O(n)

0 commit comments

Comments
ย (0)