-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
118 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)_ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |