Advertisement

Valid Palindrome II - LeetCode 680 Solution

Valid Palindrome II - Complete Solution Guide

Valid Palindrome II is LeetCode problem 680, a Easy level challenge. This complete guide provides step-by-step explanations, multiple solution approaches, and optimized code in python3, java, cpp, c.

Problem Statement

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 Constraints: 1 <= s.length <= 10 5 s consists of lowercase English letters.

Detailed Explanation

The problem asks us to determine if a given string `s` can be made a palindrome by deleting at most one character. A palindrome is a string that reads the same forwards and backward. The input is a string `s` consisting of lowercase English letters. The output is a boolean value: `true` if the string can become a palindrome after deleting at most one character, and `false` otherwise. The constraints are that the string length is between 1 and 10^5.

Solution Approach

The solution uses a two-pointer approach with a helper function. The main function iterates through the string using two pointers, `l` and `r`, starting from the beginning and end respectively. If the characters at these pointers match, they are incremented/decremented. If they don't match, it means we need to delete one of these characters. The solution then calls the `isPalindrome` helper function twice: once with `l + 1, r` (deleting the left character) and once with `l, r - 1` (deleting the right character). If either of these calls returns `true`, the original string can be made a palindrome by deleting at most one character. If the initial loop completes without finding any mismatches, the string is already a palindrome.

Step-by-Step Algorithm

  1. Step 1: Initialize two pointers, `l` and `r`, to the start and end of the string, respectively.
  2. Step 2: While `l` is less than `r`, compare the characters at `s[l]` and `s[r]`.
  3. Step 3: If `s[l]` and `s[r]` are not equal, call the `isPalindrome` helper function twice: once with `l + 1, r` and once with `l, r - 1`. Return `true` if either of these calls returns `true`.
  4. Step 4: If `s[l]` and `s[r]` are equal, increment `l` and decrement `r`.
  5. Step 5: If the loop completes without finding any mismatches, return `true` (the string is already a palindrome).
  6. Step 6: The `isPalindrome` helper function takes a string and two pointers, `l` and `r`, and checks if the substring between `l` and `r` is a palindrome. It returns `true` if it is, and `false` otherwise.

Key Insights

  • Insight 1: The problem can be solved using the two-pointer approach to check if a string is a palindrome. If a mismatch is found, we explore two possibilities: deleting the left character or deleting the right character.
  • Insight 2: The core idea is to identify the first mismatch when checking for a palindrome and then recursively check if the string becomes a palindrome after deleting either the left or the right character that caused the mismatch.
  • Insight 3: The `isPalindrome` helper function helps to abstract out the palindrome checking logic, making the code cleaner and easier to understand.

Complexity Analysis

Time Complexity: O(n)

Space Complexity: O(1)

Topics

This problem involves: Two Pointers, String, Greedy.

Companies

Asked at: Attentive, Fortinet, Roku, Walmart Labs, Whatnot, Yandex, eBay.