Skip to content

Commit 33875e8

Browse files
authored
Merge pull request #428 from seona926/main
[Sophia] Week4 Solution
2 parents 2bcb30a + dd36993 commit 33875e8

File tree

1 file changed

+36
-0
lines changed

1 file changed

+36
-0
lines changed

โ€Žvalid-palindrome/seona926.js

+36
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
/**
2+
* @param {string} s
3+
* @return {boolean}
4+
*/
5+
var isPalindrome = function (s) {
6+
// 1. ์•„๋ž˜ ๋ฐฉ์‹์€ O(n^2)์— ์ˆ˜๋ ดํ•จ
7+
// s์˜ ๊ตฌ์„ฑ์š”์†Œ๋“ค์„ ์†Œ๋ฌธ์ž๋กœ ๋ณ€ํ™˜ ํ›„ ๋ฐฐ์—ด์— ๋„ฃ๊ณ 
8+
// ํ˜„์žฌ ๊ธธ์ด๊ฐ€ 2 ์ด์ƒ์ด๋ผ๋ฉด
9+
// shift === pop ์ด๋ฉด true ์•„๋‹ˆ๋ฉด false
10+
11+
// 2. ํˆฌ ํฌ์ธํ„ฐ ์‚ฌ์šฉ
12+
13+
let str = s.toLowerCase().replace(/[^a-z0-9]/g, "");
14+
15+
// ๋‘ ํฌ์ธํ„ฐ ์‚ฌ์šฉ: ์‹œ์ž‘๊ณผ ๋์—์„œ๋ถ€ํ„ฐ ๋น„๊ต
16+
let left = 0;
17+
let right = str.length - 1;
18+
19+
while (left < right) {
20+
if (str[left] !== str[right]) {
21+
return false; // ๋‘ ๋ฌธ์ž๊ฐ€ ๋‹ค๋ฅด๋ฉด palindrome ์•„๋‹˜
22+
}
23+
24+
left++;
25+
right--;
26+
}
27+
28+
return true;
29+
};
30+
31+
/*
32+
1. ์‹œ๊ฐ„๋ณต์žก๋„: O(n)
33+
- toLowerCase, replace, ํฌ์ธํ„ฐ ์‚ฌ์šฉ์— O(n) ์†Œ์š”๋จ
34+
2. ๊ณต๊ฐ„๋ณต์žก๋„: O(n)
35+
- ๋ณ€์ˆ˜ str์ด O(n) ์ฐจ์ง€ํ•จ
36+
*/

0 commit comments

Comments
ย (0)