125. Valid Palindrome | Compare with Reverse | O(N) | LeetCode
Learn how to check if a string is a valid palindrome by comparing it with its reverse in O(N) time. Perfect for coding interviews! 🔍

Software Interviews Prep
77 views • May 24, 2024

About this video
Welcome to Software Interview Prep! Our channel is dedicated to helping software engineers prepare for coding interviews and land their dream jobs. We provide expert tips and insights on everything from data structures and algorithms to system design and behavioral questions. Whether you're just starting out in your coding career or you're a seasoned pro looking to sharpen your skills, our videos will help you ace your next coding interview. Join our community of aspiring engineers and let's conquer the tech interview together!
----------------------------------------------------------------------------------------------------------------------------------------
My Solution : https://leetcode.com/problems/valid-palindrome/submissions/1266652937?envType=study-plan-v2&envId=top-interview-150
----------------------------------------------------------------------------------------------------------------------------------------
### Code Description
```javascript
var isPalindrome = function (s) {
let filteredChars = "";
for (let ch of s) {
if (ch.match(/[a-z0-9]/i)) {
filteredChars += ch.toLowerCase();
}
}
const reversedChars = filteredChars.split("").reverse().join("");
return filteredChars === reversedChars;
};
```
### Explanation
1. **Filtering Alphanumeric Characters:**
- The function initializes an empty string `filteredChars` to store the processed characters.
```javascript
let filteredChars = "";
```
2. **Iterating Through the String:**
- A `for...of` loop iterates over each character `ch` in the input string `s`.
```javascript
for (let ch of s) {
```
3. **Checking for Alphanumeric Characters:**
- For each character `ch`, the function uses a regular expression `/[a-z0-9]/i` to check if `ch` is an alphanumeric character (letters or digits).
- The `/i` flag makes the regular expression case-insensitive.
```javascript
if (ch.match(/[a-z0-9]/i)) {
```
4. **Converting to Lowercase and Appending:**
- If `ch` is an alphanumeric character, it is converted to lowercase and appended to `filteredChars`.
```javascript
filteredChars += ch.toLowerCase();
```
5. **Reversing the Filtered String:**
- The `filteredChars` string is split into an array of characters, reversed, and then joined back into a string to form `reversedChars`.
```javascript
const reversedChars = filteredChars.split("").reverse().join("");
```
6. **Checking for Palindrome:**
- The function checks if the `filteredChars` string is equal to the `reversedChars` string. If they are equal, it means the original string `s` is a palindrome when considering only alphanumeric characters and ignoring cases.
```javascript
return filteredChars === reversedChars;
```
### Example
Let's go through an example to see how the function works.
For the input string `"A man, a plan, a canal: Panama"`:
1. **Filtering and Lowercasing:**
- The filtered and lowercased string will be: `"amanaplanacanalpanama"`
2. **Reversing the Filtered String:**
- The reversed string will be: `"amanaplanacanalpanama"`
3. **Comparison:**
- The filtered string and the reversed string are equal.
- Therefore, the function will return `true`, indicating that the input string is a palindrome.
----------------------------------------------------------------------------------------------------------------------------------------
My Solution : https://leetcode.com/problems/valid-palindrome/submissions/1266652937?envType=study-plan-v2&envId=top-interview-150
----------------------------------------------------------------------------------------------------------------------------------------
### Code Description
```javascript
var isPalindrome = function (s) {
let filteredChars = "";
for (let ch of s) {
if (ch.match(/[a-z0-9]/i)) {
filteredChars += ch.toLowerCase();
}
}
const reversedChars = filteredChars.split("").reverse().join("");
return filteredChars === reversedChars;
};
```
### Explanation
1. **Filtering Alphanumeric Characters:**
- The function initializes an empty string `filteredChars` to store the processed characters.
```javascript
let filteredChars = "";
```
2. **Iterating Through the String:**
- A `for...of` loop iterates over each character `ch` in the input string `s`.
```javascript
for (let ch of s) {
```
3. **Checking for Alphanumeric Characters:**
- For each character `ch`, the function uses a regular expression `/[a-z0-9]/i` to check if `ch` is an alphanumeric character (letters or digits).
- The `/i` flag makes the regular expression case-insensitive.
```javascript
if (ch.match(/[a-z0-9]/i)) {
```
4. **Converting to Lowercase and Appending:**
- If `ch` is an alphanumeric character, it is converted to lowercase and appended to `filteredChars`.
```javascript
filteredChars += ch.toLowerCase();
```
5. **Reversing the Filtered String:**
- The `filteredChars` string is split into an array of characters, reversed, and then joined back into a string to form `reversedChars`.
```javascript
const reversedChars = filteredChars.split("").reverse().join("");
```
6. **Checking for Palindrome:**
- The function checks if the `filteredChars` string is equal to the `reversedChars` string. If they are equal, it means the original string `s` is a palindrome when considering only alphanumeric characters and ignoring cases.
```javascript
return filteredChars === reversedChars;
```
### Example
Let's go through an example to see how the function works.
For the input string `"A man, a plan, a canal: Panama"`:
1. **Filtering and Lowercasing:**
- The filtered and lowercased string will be: `"amanaplanacanalpanama"`
2. **Reversing the Filtered String:**
- The reversed string will be: `"amanaplanacanalpanama"`
3. **Comparison:**
- The filtered string and the reversed string are equal.
- Therefore, the function will return `true`, indicating that the input string is a palindrome.
Tags and Topics
Browse our collection to discover more content in these categories.
Video Information
Views
77
Likes
2
Duration
7:29
Published
May 24, 2024
Related Trending Topics
LIVE TRENDSRelated trending topics. Click any trend to explore more videos.
Trending Now