0% completed
Problem Statement
Given string s
, determine whether it's possible to make a given string palindrome by removing at most one character.
A palindrome is a word or phrase that reads the same backward as forward.
Examples
-
Example 1:
- Input:
"racecar"
- Expected Output:
true
- Justification: The string is already a palindrome, so no removals are needed.
- Input:
-
Example 2:
- Input:
"abeccdeba"
- Expected Output:
true
- Justification: Removing the character 'd' forms the palindrome "abccba".
- Input:
-
Example 3:
- Input:
"abcdef"
- Expected Output:
false
- Justification: No single character removal will make this string a palindrome.
- Input:
Constraints:
- 1 <= s.length <= 10<sup>5</sup>
str
consists of lowercase English letters.
Solution
To solve this problem, we use a two-pointer approach that initiates at both ends of the string. These pointers move towards the center, comparing characters at each step. Upon encountering a mismatch, the algorithm decides whether to skip the character at the left or the right pointer. A helper function is used to check if the resulting substring (after skipping a character) forms a palindrome. This process is performed twice, once for each pointer. If either scenario results in a palindrome, the original string can be considered a valid palindrome after removing at most one character. This efficient method determines the feasibility of forming a palindrome with minimal alterations to the string.
-
Initialization: Begin by initializing the
left
pointer with 0 and theright
pointer with n - 1, where n is a string length. -
Two-Pointer Traversal: Use two pointers, and move these pointers towards the center, comparing the characters at each step.
-
Handling Mismatch: Upon encountering a mismatch, the algorithm checks two scenarios: removing the character at the left pointer or at the right pointer. For each scenario, it checks if the remaining substring forms a palindrome.
-
Greedy Decision Making: If either resulting substring is a palindrome, return true. This decision is based on the greedy principle that choosing the first viable option (resulting in a palindrome) is sufficient.
-
Concluding Result: If neither scenario results in a palindrome, the algorithm concludes that it's impossible to form a palindrome by removing just one character and returns false.
This greedy approach is efficient as it minimizes the number of checks needed to determine if the string can be a valid palindrome with a single character removal.
Algorithm Walkthrough
Input:- "abeccdeba"
-
Initial Setup:
- Two pointers are initialized:
left
at the start (pointing to'a'
) andright
at the end (pointing to'a'
).
- Two pointers are initialized:
-
First Iteration:
- Compare characters at
left
andright
pointers. - Characters are the same (
'a'
), so moveleft
to the right andright
to the left.
- Compare characters at
-
Second Iteration:
- Now
left
points to'b'
andright
points to'b'
. - Characters match, so move
left
andright
inward again.
- Now
-
Third Iteration:
left
is now at'e'
, andright
is at'e'
.- Characters match, so move
left
andright
inward again.
-
Fourth Iteration:
left
is now at'c'
, andright
is at'd'
.- Characters do not match. This is where a decision is made.
-
Checking Substrings:
- Remove the character at the
left
pointer ('c'
) and check if the substring"cd"
is a palindrome. It is not. - Remove the character at the
right
pointer ('d'
) and check if the substring"cc"
is a palindrome. It is a palindrome.
- Remove the character at the
-
Conclusion:
- Since removing the character
'd'
(at theright
pointer) resulted in a palindrome, the answer istrue
.
- Since removing the character
Code
Here is the code for this algorithm:
Complexity Analysis
- Time Complexity: O(n) for traversing the string.
- Space Complexity: O(1) as no extra space is used.
.....
.....
.....
Table of Contents
Contents are not accessible
Contents are not accessible
Contents are not accessible
Contents are not accessible
Contents are not accessible