Skip to content

Commit b555e1d

Browse files
committed
feat: add valid palindrome solution
1 parent 3900ba6 commit b555e1d

File tree

4 files changed

+118
-0
lines changed

4 files changed

+118
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
| 9 | [Maximum Product of Three Numbers](https://leetcode.com/problems/maximum-product-of-three-numbers/) | [TypeScript](https://github.com/sandrig/leetcode/blob/master/typescript/src/maximumProductofThreeNumbers/README.md) | _O(nlogn)_ | _O(1)_ | Array, Math, Sorting |
1616
| 10 | [Maximum Average Subarray I](https://leetcode.com/problems/maximum-average-subarray-i/) | [TypeScript](https://github.com/sandrig/leetcode/blob/master/typescript/src/maximumAverageSubarrayI/README.md) | _O(n)_ | _O(1)_ | Array, Sliding Window |
1717
| 11 | [Intersection of Two Arrays II](https://leetcode.com/problems/intersection-of-two-arrays-ii/) | [TypeScript](https://github.com/sandrig/leetcode/blob/master/typescript/src/intersectionOfTwoArraysII/README.md) | _O(n + m)_ | _O(min(n, m))_ | Array, Hash Table, Two Pointers, Binary Search, Sorting |
18+
| 12 | [Valid Palindrome II](https://leetcode.com/problems/valid-palindrome-ii/) | [TypeScript](https://github.com/sandrig/leetcode/blob/master/typescript/src/validPalindromeII/README.md) | _O(n)_ | _O(1)_ | Two Pointers, String Greedy |
1819

1920
## Medium
2021

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
# [Valid Palindrome II](https://leetcode.com/problems/valid-palindrome-ii/)
2+
3+
## Description
4+
5+
Given a string **s**, return **true** if the **s** can be palindrome after deleting at most one character from it.
6+
7+
**Example 1:**
8+
9+
```
10+
Input: s = "aba"
11+
Output: true
12+
```
13+
14+
**Example 2:**
15+
16+
```
17+
Input: s = "abca"
18+
Output: true
19+
Explanation: You could delete the character 'c'.
20+
```
21+
22+
**Example 3:**
23+
24+
```
25+
Input: s = "abc"
26+
Output: false
27+
```
28+
29+
## Solution
30+
31+
```typescript
32+
function validPalindrome(s: string): boolean {
33+
function isPalindromeRange(start: number, end: number): boolean {
34+
while (start < end) {
35+
if (s[start] !== s[end]) {
36+
return false;
37+
}
38+
start++;
39+
end--;
40+
}
41+
return true;
42+
}
43+
44+
let left = 0;
45+
let right = s.length - 1;
46+
47+
while (left < right) {
48+
if (s[left] !== s[right]) {
49+
// Try skipping the left character or the right character
50+
return (
51+
isPalindromeRange(left + 1, right) || isPalindromeRange(left, right - 1)
52+
);
53+
}
54+
left++;
55+
right--;
56+
}
57+
58+
return true;
59+
}
60+
```
61+
62+
## Complexity Analysis
63+
64+
- Time complexity: _O(n)_
65+
- Space complexity: _O(1)_
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
import { validPalindrome } from "./validPalindrome";
2+
3+
describe("Valid Palindrome II", () => {
4+
it("should return true for a valid palindrome", () => {
5+
expect(validPalindrome("aba")).toBe(true);
6+
expect(validPalindrome("abccba")).toBe(true);
7+
expect(validPalindrome("racecar")).toBe(true);
8+
expect(validPalindrome("deified")).toBe(true);
9+
});
10+
11+
it("should return true for a valid palindrome when one character is removed", () => {
12+
expect(validPalindrome("abca")).toBe(true);
13+
expect(validPalindrome("abcba")).toBe(true);
14+
expect(validPalindrome("abccbad")).toBe(true);
15+
expect(validPalindrome("radarz")).toBe(true);
16+
});
17+
18+
it("should return false when more than one character is removed", () => {
19+
expect(validPalindrome("abcde")).toBe(false);
20+
expect(validPalindrome("levellee")).toBe(false);
21+
expect(validPalindrome("radarza")).toBe(false);
22+
expect(validPalindrome("racecarara")).toBe(false);
23+
});
24+
});
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
export function validPalindrome(s: string): boolean {
2+
function isPalindromeRange(start: number, end: number): boolean {
3+
while (start < end) {
4+
if (s[start] !== s[end]) {
5+
return false;
6+
}
7+
start++;
8+
end--;
9+
}
10+
return true;
11+
}
12+
13+
let left = 0;
14+
let right = s.length - 1;
15+
16+
while (left < right) {
17+
if (s[left] !== s[right]) {
18+
// Try skipping the left character or the right character
19+
return (
20+
isPalindromeRange(left + 1, right) || isPalindromeRange(left, right - 1)
21+
);
22+
}
23+
left++;
24+
right--;
25+
}
26+
27+
return true;
28+
}

0 commit comments

Comments
 (0)