Skip to content

Commit d76ef6b

Browse files
committed
✨ [125] Valid Palindrome
1 parent 5c9df93 commit d76ef6b

File tree

4 files changed

+66
-1
lines changed

4 files changed

+66
-1
lines changed

125/my_solution.js

+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
/**
2+
* @param {string} s
3+
* @return {boolean}
4+
*/
5+
const isPalindrome = (s) => {
6+
// process the string to trim &
7+
// gpt why what is this fking regex
8+
s = s.replace(/[^a-zA-Z0-9]+/g, '').trim().toLowerCase();
9+
//s= s.replace(/[a-zA-z]/g);
10+
console.log(s)
11+
// for (let i = 0; i < s.length; i++) {
12+
13+
let l = 0, r = s.length - 1;
14+
15+
while (l <= r) {
16+
17+
if (s[l] !== s[r]) {
18+
return false;
19+
}
20+
21+
l++;
22+
r--;
23+
}
24+
25+
return true;
26+
}
27+
28+
// let x = isPalindrome("A man, a plan, a canal: Panama")
29+
// let x = isPalindrome("A man, a plan, a canal: Panama")
30+
let x = isPalindrome("0P")
31+
console.log("result")
32+
console.log(x)

125/solution.js

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
/**
2+
* @param {string} s
3+
* @return {boolean}
4+
*/
5+
const isPalindrome = (s) => {
6+
s = s.replace(/[^a-zA-Z0-9]+/g, '').trim().toLowerCase();
7+
let l = 0, r = s.length - 1;
8+
9+
while (l <= r) {
10+
if (s[l] !== s[r]) return false;
11+
12+
l++;
13+
r--;
14+
}
15+
16+
return true;
17+
}

README.md

+2-1
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
- [76. Minimum Window Substring](./76/)
3131
- [83. Remove Duplicates from Sorted List](./83/)
3232
- [121. Best Time to Buy and Sell Stock](./121/)
33+
- [125. Valid Palindrome](./125/)
3334
- [141. Linked List Cycle](./141/)
3435
- [143. Reorder List](./143/)
3536
- [152. Maximum Product Subarray](./152/)
@@ -81,5 +82,5 @@ Batch create in bash
8182
TODO: Add to TOC!
8283
-->
8384
```ssh
84-
chapter=76 && mkdir ./$chapter && touch ./$chapter/my_solution.js && touch ./$chapter/solution.js && alias x="node ./$chapter/my_solution.js"
85+
chapter=125 && mkdir ./$chapter && touch ./$chapter/my_solution.js && touch ./$chapter/solution.js && alias x="node ./$chapter/my_solution.js"
8586
```

test.js

+15
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,18 @@ if (obj["a"]) {
55
} else {
66
console.log("n")
77
}
8+
9+
10+
11+
function extractWords(str) {
12+
// replace(/[^\p{L}\s]/gu, '').replace(/\s+/g, ' ').trim()
13+
// return str.replace(/[^a-zA-Z\s]/g, '').replace(/\s+/g, ' ').trim();
14+
return str.replace(/[^a-zA-Z]+/g, '').trim();
15+
return str.replace(/[^a-zA-Z\s]+/g, ' ').trim();
16+
17+
}
18+
19+
20+
// let str ="qw1 19jod12901j2@J(!@#"
21+
let str = "A man, a plan, a canal: Panama";
22+
console.log(extractWords(str));

0 commit comments

Comments
 (0)