Skip to content

Commit e98b388

Browse files
committed
valid-palindrome solution
1 parent cd23f6a commit e98b388

File tree

1 file changed

+77
-0
lines changed

1 file changed

+77
-0
lines changed

valid-palindrome/Blossssom.ts

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
/**
2+
* @param s - 입력 문자열
3+
* @returns - 입력 문자열이 palindrome 인지 반환
4+
* @description
5+
* 풀이 1 - 시간복잡도: O(n), 공간복잡도: O(n)
6+
* 풀이 2 - 시간복잡도: O(n), 공간복잡도: O(1) - 초반 정규식 스캔 및 소문자 변경이 한번에 일어나지 않고, 조기 종료
7+
* 풀이 3 - 1번과 복잡도는 같지만 split으로 실제 실행시간은 훨씬 빠르게 진행
8+
*/
9+
// function isPalindrome(s: string): boolean {
10+
// const clearString = s.replace(/[^a-zA-Z0-9]/g, "").toLocaleLowerCase();
11+
// let strLen = clearString.length - 1;
12+
// if (!clearString.length) {
13+
// return true;
14+
// }
15+
16+
// for (let i = 0; i < Math.floor(clearString.length / 2); i++) {
17+
// if (clearString[i] !== clearString[strLen]) {
18+
// return false;
19+
// }
20+
// strLen--;
21+
// }
22+
// return true;
23+
// }
24+
25+
// function isPalindrome(s: string): boolean {
26+
// let i = 0;
27+
// let j = s.length - 1;
28+
29+
// while (i < j) {
30+
// if (!isAlphaNum[s[i]]) {
31+
// i++;
32+
// continue;
33+
// }
34+
35+
// if (!isAlphaNum[s[j]]) {
36+
// j--;
37+
// continue;
38+
// }
39+
40+
// if (s[i].toLocaleLowerCase() !== s[j].toLocaleLowerCase()) {
41+
// return false;
42+
// }
43+
44+
// i++;
45+
// j--;
46+
// }
47+
// return true;
48+
// }
49+
50+
// function isAlphaNum(str: string): boolean {
51+
// const charC = str.charCodeAt(0);
52+
// return (
53+
// (charC >= "0".charCodeAt(0) && charC <= "9".charCodeAt(0)) ||
54+
// (charC >= "A".charCodeAt(0) && charC <= "Z".charCodeAt(0)) ||
55+
// (charC >= "a".charCodeAt(0) && charC <= "z".charCodeAt(0))
56+
// );
57+
// }
58+
59+
function isPalindrome(s: string): boolean {
60+
const cleaned = s
61+
.toLowerCase()
62+
.replace(/[^a-zA-Z0-9]/g, "")
63+
.split("");
64+
let left = 0;
65+
let right = cleaned.length - 1;
66+
while (left < right) {
67+
if (cleaned[left] === cleaned[right]) {
68+
left++;
69+
right--;
70+
} else {
71+
return false;
72+
}
73+
}
74+
return true;
75+
}
76+
77+

0 commit comments

Comments
 (0)