Valid Palindrome II
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: trueExample 2:
Input: s = "abca"
Output: true
Explanation: You could delete the character 'c'.Example 3:
Input: s = "abc"
Output: falseConstraints:
The input string
sconsists only of printable ASCII characters.The string
scan be empty.The string
scan contain spaces, punctuation, and other non-alphanumeric characters.The string
sonly lowercase letters.
Questions to Clarify:
Non-Alphanumeric Characters: Should non-alphanumeric characters be ignored when checking for a palindrome? The problem statement suggests they should be, but again, it would be good to confirm this.
Null Input: How should the function handle a null input? Should it return
true(since a null string can be considered a special case of an empty string),false, or should it throw an exception?Large Inputs: What is the maximum length of the input string
s? This could affect the choice of algorithm, especially for very long strings.Performance Requirements: Are there any performance requirements or constraints? For example, does the function need to run in a certain amount of time or use a certain amount of memory?
Solutions
The provide string may or may not palindrome by default. We may need to delete at most one character to make string palindrome.
Approach - Brute Force Technique
In this problem is that I will use two pointers so that we can check if the string is palindrome after excluding one of the chars.
Steps
Start of the function: The
public bool ValidPalindrome(string s)line defines a public function namedValidPalindromethat takes a stringsas input and returns a boolean value.Start of the loop: The
for (int i = 0; i < s.Length; i++)line starts a loop that iterates over each character in the strings. The variableiis the loop counter and represents the index of the current character.Remove a character and check for palindrome: The
if (IsPalindrome(s.Remove(i, 1)))line inside the loop does two things:s.Remove(i, 1)removes the character at indexifrom the strings. This creates a new string that is the same assbut without the character at indexi.IsPalindrome(...)checks if the resulting string is a palindrome. This is done by calling theIsPalindromefunction, which is not shown in the provided code but is assumed to check if a string is a palindrome.
Return true if a palindrome is found: If the
IsPalindromefunction returnstrue, indicating that the string is a palindrome after removing the character at indexi, theValidPalindromefunction immediately returnstrue.Return false if no palindrome is found: If the loop has iterated over all characters in the string
sand hasn't found a palindrome, theValidPalindromefunction returnsfalse.
public class Solution
{
public bool ValidPalindrome(string s)
{
for (int i = 0; i < s.Length; i++)
{
if (IsPalindrome(s.Remove(i, 1)))
{
return true;
}
}
return false;
}
public bool IsPalindrome(string s)
{
int i = 0, j = s.Length - 1;
while (i < j)
{
if (s[i++] != s[j--])
{
return false;
}
}
return true;
}
}Complexity
Time complexity:
O(N^2)Space complexity:
O(1)
Approach - Two pointers Technique
In this problem is that I will use two pointers so that we can check if the string is palindrome after excluding one of the chars.
Steps
Initialize two pointers: The
int left = 0, right = s.Length - 1;line initializes two pointers,leftandright.leftstarts at the beginning of the stringsandrightstarts at the end.Start of the loop: The
while (left < right)line starts a loop that continues as long asleftis less thanright. This means the loop will continue until the two pointers meet or cross each other.Compare characters at the pointers: The
if (s[left] == s[right])line inside the loop compares the characters at theleftandrightpositions. If they are the same, it incrementsleftand decrementsright, moving the pointers towards each other.Check for palindrome after removing a character: If the characters at the
leftandrightpositions are not the same, theelseblock is executed. This block returns the result ofIsPalindrome(s, left + 1, right) || IsPalindrome(s, left, right - 1). This line checks if the stringswould be a palindrome if either the character at theleftposition or the character at therightposition were removed. It does this by calling theIsPalindromefunction, which is not shown in the provided code but is assumed to check if a string is a palindrome.Return true if no mismatches are found: If the loop has checked all pairs of characters and hasn't found any mismatches, the function returns
true, indicating the string is a palindrome.
public class Solution {
public bool ValidPalindrome(string s) {
int left = 0, right = s.Length - 1;
while (left < right){
if (s[left] == s[right]){
left++;
right--;
}
else
{
// wost case 2N times execute left || right case.
return IsPalindrome(s, left + 1, right) || IsPalindrome(s, left, right - 1);
}
}
return true;
}
private bool IsPalindrome(string s, int left, int right){
while (left < right){
if (s[left] != s[right])
return false;
left++;
right--;
}
return true;
}
}Complexity
Time complexity:
O(N)Space complexity:
O(1)
Last updated