Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -78,4 +78,6 @@
- [Task #6](src/neetcode/arrays-&-hashing/encode-and-decode-strings) - _Encode And Decode Strings_ - _[check the task](https://neetcode.io/problems/string-encode-and-decode)_
- [Task #7](src/neetcode/arrays-&-hashing/product-of-array-except-self) - _Products of Array Except Self_ - _[check the task](https://neetcode.io/problems/products-of-array-discluding-self)_
- [Task #8](src/neetcode/arrays-&-hashing/valid-sudoku) - _Valid Sudoku_ - _[check the task](https://neetcode.io/problems/valid-sudoku)_
- [Task #9](src/neetcode/arrays-&-hashing/longest-consecutive-sequence) - _Longest Consecutive Sequence_ - _[check the task](https://neetcode.io/problems/longest-consecutive-sequence)_
- [Task #9](src/neetcode/arrays-&-hashing/longest-consecutive-sequence) - _Longest Consecutive Sequence_ - _[check the task](https://neetcode.io/problems/longest-consecutive-sequence)_
- Two Pointers
- [Task #10](src/neetcode/two-pointers/valid-palindrome) - _Valid Palindrome_ - _[check the task](https://neetcode.io/problems/is-palindrome)_
13 changes: 13 additions & 0 deletions src/neetcode/two-pointers/valid-palindrome/solution.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { isPalindrome } from './solution';

describe('Valid Palindrome | NeetCode RoadMap | Testcases', () => {
test('#1 Valid string', () => {
const s = 'Was it a car or a cat I saw?';
expect(isPalindrome(s)).toBe(true);
});

test('#2 Invalid string', () => {
const s = 'tab a cat';
expect(isPalindrome(s)).toBe(false);
});
});
25 changes: 25 additions & 0 deletions src/neetcode/two-pointers/valid-palindrome/solution.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/**
* @param {string} s - input string
* @return {boolean} `true` if `s` is a palindrome, false otherwise
*/
export const isPalindrome = (s: string): boolean => {
const isAlphaNumeric = (char: string): boolean =>
(char >= 'a' && char <= 'z') || (char >= 'A' && char <= 'Z') || (char >= '0' && char <= '9');

let right = s.length - 1;
let left = 0;
while (left < right) {
while (left < right && !isAlphaNumeric(s[left])) {
left++;
}
while (left < right && !isAlphaNumeric(s[right])) {
right--;
}
if (s[left].toLowerCase() !== s[right].toLowerCase()) {
return false;
}
left++;
right--;
}
return true;
};
Loading