## My LeetCode: https://leetcode-cn.com/u/ituring/

## My LeetCode source code [GitHub]: https://github.com/izhoujie/Algorithmcii

## LeetCode 680. Verify palindrome string II

# subject

Given a non empty string s, delete at most one character. Determine whether it can be a palindrome string.

Example 1:

Enter: "aba" Output: True

Example 2:

Enter: "abca" Output: True Explanation: you can delete the c character.

be careful:

- The string contains only lowercase letters from a-z.
- The maximum length of the string is 50000.

Source: LeetCode

Link: https://leetcode-cn.com/problems/valid-palindrome-ii

Copyright belongs to the network. For commercial reprint, please contact the official authorization. For non-commercial reprint, please indicate the source.

## Solutions to problems

If at most one is deleted, there are three situations when the left and right pointers are forced to verify:

- No need to delete;
- In case of inequality, delete one character on the left and continue the verification;
- In case of inequality, delete one character on the right and continue the verification;

In the three cases, the verification mode is the same except for the difference of initial pointer position;

### Idea 1 - record the position of pointer and refine the verification mode method

At most three mode verifications are needed, but the starting position of the pointer for three mode verifications needs to be recorded;

Specify the location of the two variable record pointers:

- If the first verification fails, the following two starting pointers are the current pointer positions:
- Move the left pointer one step to the right to continue the verification, if the verification fails;
- Move the right pointer one step to the left to continue the verification;

Algorithm complexity:

- Time complexity: ${\ color{Magenta}{\Omicron\left(n\right)}}}$
- Spatial complexity: ${\ color{Magenta}{\Omicron\left(1\right)}}$

### Idea 2 - recursive verification

For the first time, it is verified as the first level of recursion, and the second level of recursion is to delete the left or right character. Therefore, recursion needs strict control, only recursion to the second level, and a boolean variable control is designed;

See code for details;

Algorithm complexity:

- Time complexity: ${\ color{Magenta}{\Omicron\left(n\right)}}}$
- Spatial complexity: ${\ color{Magenta}{\Omicron\left(1\right)}} $stack depth is 2, constant level

### Idea 3 - solve directly in a single method and a while

When the verification fails for the first time, the left deletion or right deletion needs to be verified. The idea here is to try the left deletion first. If the verification fails for the left deletion, let the left pointer back one position to the left on the basis of the current pointer position, move the right pointer forward one position to the left, and then continue the verification, balancing the distance of moving more right when the left is deleted first;

Algorithm complexity:

- Time complexity: ${\ color{Magenta}{\Omicron\left(n\right)}}}$
- Spatial complexity: ${\ color{Magenta}{\Omicron\left(1\right)}}$

## Algorithm source code example

package leetcode; /** * @author ZhouJie * @date 2020 12:24:57 am, May 19, 2015 * @Description: 680. Verify palindrome string II * */ public class LeetCode_0680 { } class Solution_0680 { /** * @author: ZhouJie * @date: 2020 12:25:24 am, May 19, 2010 * @param: @param s * @param: @return * @return: boolean * @Description: 1-Two pointer traversal, recording the pointer position, because there are three opportunities to verify whether it can be transformed into palindrome; * */ private int start = 0; private int end = 0; public boolean validPalindrome_1(String s) { end = s.length() - 1; if (!check(s)) { int ss = start, ee = end; start++; if (!check(s)) { start = ss; end = ee; end--; return check(s); } } return true; } private boolean check(String s) { if (start > end) { return false; } else { while (start <= end) { if (s.charAt(start) == s.charAt(end)) { start++; end--; } else { return false; } } return true; } } /** * @author: ZhouJie * @date: 2020 12:52:52 am, May 19, 2015 * @param: @param s * @param: @return * @return: boolean * @Description: 2-Recursive verification, control only recursion two layers; * */ public boolean validPalindrome_2(String s) { return check(s, 0, s.length() - 1, true); } private boolean check(String s, int start, int end, boolean f) { while (start < end) { if (s.charAt(start) == s.charAt(end)) { start++; end--; } else { // If it is the first time, delete the left or right and verify again, otherwise it means that it cannot be transformed into palindrome return f && (check(s, start + 1, end, false) || check(s, start, end - 1, false)); } } return true; } /** * @author: ZhouJie * @date: 2020 1:09:12 am, May 19, 2010 * @param: @param s * @param: @return * @return: boolean * @Description: 3-Adding variables to a single method; * */ public boolean validPalindrome_3(String s) { int left = 0, right = s.length() - 1; boolean leftDelete = true, rightDelete = true; while (left < right) { if (s.charAt(left) == s.charAt(right)) { left++; right--; } else { // Try to delete the left if (leftDelete) { left++; leftDelete = false; // Try to delete the right side, and the left side will back one character } else if (rightDelete) { left--; right--; rightDelete = false; } else { return false; } } } return true; } }