Valid Palindrome II
String, Two Pointers
Given a string s
, return true if the s
can be palindrome after deleting at most one character from it.
Example 1:
Example 2:
Example 3:
Constraints:
The input string
s
consists only of printable ASCII characters.The string
s
can be empty.The string
s
can contain spaces, punctuation, and other non-alphanumeric characters.The string
s
only 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 namedValidPalindrome
that takes a strings
as 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 variablei
is 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 indexi
from the strings
. This creates a new string that is the same ass
but without the character at indexi
.IsPalindrome(...)
checks if the resulting string is a palindrome. This is done by calling theIsPalindrome
function, 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
IsPalindrome
function returnstrue
, indicating that the string is a palindrome after removing the character at indexi
, theValidPalindrome
function immediately returnstrue
.Return false if no palindrome is found: If the loop has iterated over all characters in the string
s
and hasn't found a palindrome, theValidPalindrome
function returnsfalse
.
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,left
andright
.left
starts at the beginning of the strings
andright
starts at the end.Start of the loop: The
while (left < right)
line starts a loop that continues as long asleft
is 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 theleft
andright
positions. If they are the same, it incrementsleft
and decrementsright
, moving the pointers towards each other.Check for palindrome after removing a character: If the characters at the
left
andright
positions are not the same, theelse
block is executed. This block returns the result ofIsPalindrome(s, left + 1, right) || IsPalindrome(s, left, right - 1)
. This line checks if the strings
would be a palindrome if either the character at theleft
position or the character at theright
position were removed. It does this by calling theIsPalindrome
function, 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.
Complexity
Time complexity:
O(N)
Space complexity:
O(1)
Last updated