Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 48cef6f

Browse files
committed
Add a question
1 parent 0e8a5f3 commit 48cef6f

File tree

1 file changed

+47
-0
lines changed

1 file changed

+47
-0
lines changed

‎README.md

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2125,3 +2125,50 @@ const toCamel = obj => {
21252125
---
21262126
21272127
**[⬆ Back to Top](#javascript-coding-challenges-for-beginners)**
2128+
2129+
## 59. Valid Palindrome
2130+
2131+
A phrase is a palindrome if, after converting all uppercase letters into lowercase letters and removing all non-alphanumeric characters, it reads the same forward and backward. Alphanumeric characters include letters and numbers. Given a string `s`, return `true` if it is a palindrome, or `false` otherwise.
2132+
2133+
```js
2134+
const isPalindrome = s => {
2135+
// Your solution
2136+
};
2137+
2138+
console.log(isPalindrome('A man, a plan, a canal: Panama')); // true
2139+
// Explanation: "amanaplanacanalpanama" is a palindrome.
2140+
console.log(isPalindrome('race a car')); // false
2141+
// Explanation: "raceacar" is not a palindrome.
2142+
console.log(isPalindrome('ab_a')); // true
2143+
// Explanation: "aba" is a palindrome.
2144+
console.log(isPalindrome(' ')); // true
2145+
// Explanation: `s` is an empty string "" after removing non-alphanumeric characters.
2146+
// Since an empty string reads the same forward and backward, it is a palindrome.
2147+
```
2148+
2149+
<details><summary>Solution</summary>
2150+
2151+
```js
2152+
const isPalindrome = s => {
2153+
const str = s.toLowerCase().replace(/[^a-z0-9]/g, '');
2154+
let start = 0;
2155+
let end = str.length - 1;
2156+
2157+
while (start < end) {
2158+
if (str[start] !== str[end]) return false;
2159+
start++;
2160+
end--;
2161+
}
2162+
return true;
2163+
2164+
// Alternative solution using built-in reverse() method
2165+
// const str = s.toLowerCase().replace(/[^a-z0-9]/g, '');
2166+
// return str === [...str].reverse().join('');
2167+
};
2168+
```
2169+
2170+
</details>
2171+
2172+
---
2173+
2174+
**[⬆ Back to Top](#javascript-coding-challenges-for-beginners)**

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /