Title: Given a string, in case a maximum of one character is deleted, the string is judged to be a palindrome string.
Idea: Palindrome string, the first thought is to use two pointers, before and after each, when encountered before and after the character inconsistency, there are two cases, delete the preceding character or delete the following character. Since the deletion of a character is still a string, it can be processed directly recursively. Then with a flag, when the 2 is reached, it is possible to end the recursion.
The code is as follows:
1 classsolution (object):2 3 defIspalindrome (self, s, left, right, flag):4 whileLeft <Right :5 ifS[left] = =S[right]:6Left + = 17Right-= 18 Else:9 ifFlag = = 1:Ten returnFalse OneFlag = 1 A return(Self.ispalindrome (S, left+1, right, flag)or -Self.ispalindrome (S, left, right-1, flag)) - returnTrue the - defValidpalindrome (self, s): - """ - : Type S:str + : Rtype:bool - """ + returnSelf.ispalindrome (S, 0, Len (s)-1, 0)
Python solution leetcode:680. Valid palindrome II