Skip to content

Commit

Permalink
feat: add valid palindrome solution
Browse files Browse the repository at this point in the history
  • Loading branch information
sandrig committed Aug 1, 2023
1 parent 3900ba6 commit b555e1d
Show file tree
Hide file tree
Showing 4 changed files with 118 additions and 0 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
| 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 |
| 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 |
| 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 |
| 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 |

## Medium

Expand Down
65 changes: 65 additions & 0 deletions typescript/src/validPalindromeII/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
# [Valid Palindrome II](https://leetcode.com/problems/valid-palindrome-ii/)

## Description

Given a string **s**, return **true** if the **s** can be palindrome after deleting at most one character from it.

**Example 1:**

```
Input: s = "aba"
Output: true
```

**Example 2:**

```
Input: s = "abca"
Output: true
Explanation: You could delete the character 'c'.
```

**Example 3:**

```
Input: s = "abc"
Output: false
```

## Solution

```typescript
function validPalindrome(s: string): boolean {
function isPalindromeRange(start: number, end: number): boolean {
while (start < end) {
if (s[start] !== s[end]) {
return false;
}
start++;
end--;
}
return true;
}

let left = 0;
let right = s.length - 1;

while (left < right) {
if (s[left] !== s[right]) {
// Try skipping the left character or the right character
return (
isPalindromeRange(left + 1, right) || isPalindromeRange(left, right - 1)
);
}
left++;
right--;
}

return true;
}
```

## Complexity Analysis

- Time complexity: _O(n)_
- Space complexity: _O(1)_
24 changes: 24 additions & 0 deletions typescript/src/validPalindromeII/validPalindrome.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import { validPalindrome } from "./validPalindrome";

describe("Valid Palindrome II", () => {
it("should return true for a valid palindrome", () => {
expect(validPalindrome("aba")).toBe(true);
expect(validPalindrome("abccba")).toBe(true);
expect(validPalindrome("racecar")).toBe(true);
expect(validPalindrome("deified")).toBe(true);
});

it("should return true for a valid palindrome when one character is removed", () => {
expect(validPalindrome("abca")).toBe(true);
expect(validPalindrome("abcba")).toBe(true);
expect(validPalindrome("abccbad")).toBe(true);
expect(validPalindrome("radarz")).toBe(true);
});

it("should return false when more than one character is removed", () => {
expect(validPalindrome("abcde")).toBe(false);
expect(validPalindrome("levellee")).toBe(false);
expect(validPalindrome("radarza")).toBe(false);
expect(validPalindrome("racecarara")).toBe(false);
});
});
28 changes: 28 additions & 0 deletions typescript/src/validPalindromeII/validPalindrome.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
export function validPalindrome(s: string): boolean {
function isPalindromeRange(start: number, end: number): boolean {
while (start < end) {
if (s[start] !== s[end]) {
return false;
}
start++;
end--;
}
return true;
}

let left = 0;
let right = s.length - 1;

while (left < right) {
if (s[left] !== s[right]) {
// Try skipping the left character or the right character
return (
isPalindromeRange(left + 1, right) || isPalindromeRange(left, right - 1)
);
}
left++;
right--;
}

return true;
}

0 comments on commit b555e1d

Please sign in to comment.