• API
• FAQ
• Tools
• Archive
SHARE
TWEET

# expand centers

a guest Aug 22nd, 2019 88 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
1. class Solution:
2.
3.     def expand_around_center(self, l, r, s):
4.         n = len(s)
5.         while l >= 0 and r < n and s[l] == s[r]:
6.             l -= 1
7.             r += 1
8.
9.         return r - l - 1
10.
11.     def longestPalindrome(self, s: str) -> str:
12.
13.         n = len(s)
14.         if n < 1:
15.             return ''
16.
17.         longest_palindrome = ''
18.         for i in range(n):
19.             len1 = self.expand_around_center(i, i, s)
20.             len2 = self.expand_around_center(i, i+1, s)
21.             max_len = max(len1, len2)
22.             if max_len > len(longest_palindrome):
23.                 start = i - (max_len -1) // 2
24.                 end = i + max_len // 2
25.                 longest_palindrome = s[start:end+1]
26.
27.         return longest_palindrome
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy.

Top