@@ -196,3 +196,82 @@ As only a few variables are used, the space complexity of this solution is O(1).
196196- String
197197- Dynamic Programming
198198- Two Pointers
199+
200+ ---
201+
202+ ## Valid Palindrome II
203+
204+ Write a function that takes a string as input and checks whether it can be a valid palindrome by removing at most one
205+ character from it.
206+
207+ ### Constraints
208+
209+ - 1 <= ` s.length ` <= 10^5
210+ - The string only consists of English letters
211+
212+ ### Examples
213+
214+ Example 1:
215+
216+ ``` text
217+ Input: s = "aba"
218+ Output: true
219+ ```
220+
221+ Example 2:
222+
223+ ``` text
224+ Input: s = "abca"
225+ Output: true
226+ Explanation: You could delete the character 'c'.
227+ ```
228+
229+ Example 3:
230+
231+ ``` text
232+ Input: s = "abc"
233+ Output: false
234+ ```
235+
236+ ### Solution
237+
238+ The algorithm uses a two-pointer technique to determine whether a string is a palindrome or can be transformed into it
239+ by removing at most one character, where one pointer starts at the beginning and the other at the end. As the pointers
240+ move toward each other, they compare the corresponding characters. If all pairs match, the string is a palindrome.
241+ However, if a mismatch is detected, the algorithm explores two possibilities: removing the character at either pointer
242+ and checking if the resulting substring forms a palindrome. If either check passes, the function returns TRUE; otherwise,
243+ it returns FALSE.
244+
245+ The algorithm consists of two functions:
246+
247+ - ` is_substring_palindrome(left, right) ` : This helper function checks if a substring of the input string, defined by
248+ ` left ` and ` right ` indexes, is a palindrome. It uses a two-pointer approach, comparing characters from both ends
249+ inward. If any mismatch is found, it returns FALSE; otherwise, it returns TRUE.
250+ - ` is_valid_palindrome_with_one_char_removal(string) ` : This function checks if the entire string is a palindrome or can
251+ become one by removing one character. It initializes two pointers, ` left_pointer ` at the start and ` right_pointer ` at
252+ end of the string:
253+ - If the characters at the ` left_pointer ` and ` right_pointer ` are the same, it moves both pointers inward.
254+ - If the characters differ, it checks two cases by calling is_substring_palindrome:
255+ - The substring from ` left_pointer + 1 ` to ` right_pointer `
256+ - The substring from ` left_pointer ` to ` right_pointer - 1 `
257+ - If either case returns TRUE, the function returns TRUE; otherwise, it returns FALSE.
258+
259+ If the traversal completes without finding a mismatch, the string is a palindrome, and the function returns TRUE.
260+
261+ ![ Solution 1] ( ./images/solutions/is_valid_palindrome_with_one_char_removal_solution_1.png )
262+ ![ Solution 2] ( ./images/solutions/is_valid_palindrome_with_one_char_removal_solution_2.png )
263+ ![ Solution 3] ( ./images/solutions/is_valid_palindrome_with_one_char_removal_solution_3.png )
264+ ![ Solution 4] ( ./images/solutions/is_valid_palindrome_with_one_char_removal_solution_4.png )
265+ ![ Solution 5] ( ./images/solutions/is_valid_palindrome_with_one_char_removal_solution_5.png )
266+ ![ Solution 6] ( ./images/solutions/is_valid_palindrome_with_one_char_removal_solution_6.png )
267+ ![ Solution 7] ( ./images/solutions/is_valid_palindrome_with_one_char_removal_solution_7.png )
268+ ![ Solution 8] ( ./images/solutions/is_valid_palindrome_with_one_char_removal_solution_8.png )
269+ ![ Solution 9] ( ./images/solutions/is_valid_palindrome_with_one_char_removal_solution_9.png )
270+
271+ #### Time Complexity
272+
273+ The time complexity of the solution above is O(n), where n is the length of the string
274+
275+ #### Space Complexity
276+
277+ The space complexity of solution above is O(1).
0 commit comments