Get the App
SLTechnology News&Howtos  ›  Internet Technology  › 

How to validate palindromes in leetcode

Shulou Source: shulou.com Published: 2022-06-01 05:03:29 10月03日 Update

This article mainly introduces how to verify palindromes in leetcode. It is very detailed and has certain reference value. Friends who are interested must finish reading it.

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

Example 1:

Enter: "aba"

Output: True

Example 2:

Enter: "abca"

Output: True

Explanation: you can delete the c character.

Note:

The string contains only lowercase letters from amerz. The maximum length of a string is 50000.

Test venue:

Palindromes have the same properties as positive and negative order.

Solution:

Start the comparison from the beginning to the end, and if it is not equal, delete one end and continue down.

Class Solution: def validPalindrome (self S: str)-> bool: # cycle to distinguish''def help (s): if s = = s [::-1]: return True else: return False if help (s): return True length = len (s) for i in range (length): If help (s [: I] + s [iTun1:]): return True return False''# starts from both ends left = 0 right = len (s)-1 while left < right: if s [left]! = s [right]: return s [left: right] = = s [left: Right] [:-1] or s [left+1: right+1] = = s [left+1: right+1] [::-1] left+ = 1 right-= 1 return True are all the contents of the article "how to verify palindromes in leetcode" Thank you for reading! Hope to share the content to help you, more related knowledge, welcome to follow the industry information channel!

Tags: Characters strings palindromes validation both ends content examples articles input output maximum one end value interest head and tail letters boys buddies lowercase properties Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Shulou Information macOS MariaDB Shulou Technology Xiaomi