
Problem
Solution
Submissions
Valid Palindrome II
Certification: Basic Level
Accuracy: 0%
Submissions: 0
Points: 8
Write a JavaScript program to determine if a string can be a palindrome after removing at most one character from it. A palindrome reads the same forwards and backwards.
Example 1
- Input: s = "aba"
- Output: true
- Explanation:
- The string "aba" is already a palindrome.
- No character removal is needed.
- Therefore, it's valid with at most one character removal.
- The string "aba" is already a palindrome.
Example 2
- Input: s = "abca"
- Output: true
- Explanation:
- The string "abca" is not a palindrome initially.
- If we remove character 'c' at index 2, we get "aba".
- The string "aba" is a palindrome.
- Therefore, removing one character makes it a valid palindrome.
- The string "abca" is not a palindrome initially.
Constraints
- 1 ≤ s.length ≤ 10^5
- s consists only of lowercase English letters
- At most one character can be removed
- Time Complexity: O(n)
- Space Complexity: O(1)
Editorial
My Submissions
All Solutions
Lang | Status | Date | Code |
---|---|---|---|
You do not have any submissions for this problem. |
User | Lang | Status | Date | Code |
---|---|---|---|---|
No submissions found. |
Solution Hints
- Use two pointers approach starting from both ends of the string
- If characters at both pointers match, move both pointers inward
- If characters don't match, try removing either the left or right character
- Check if the remaining substring is a palindrome after removal
- Create a helper function to check if a substring is a palindrome
- Return true if removing one character results in a palindrome, false otherwise