Python method to find the longest palindrome violence

Find the longest palindrome substring

Given a string s, s to find the longest substring palindromic. You can assume that the maximum length of 1000 s.

For example 1:

输入: "babad"
输出: "bab"
注意: "aba"也是一个有效答案。

Example 2:

输入: "cbbd"
输出: "bb"

The easiest way is to force solution to determine the scope of the substring by double loop, then determine substring is not palindromic, and finally returned to the longest substring palindromic.

class Solution:
    @classmethod
    def long_lca(cls, str1):
        """
        :type str1: str
        :rtype: str
        """
        max_len, result = float("-inf"), ""
        for i in range(len(str1)):
            for j in range(i+1, len(str1)):
                if str1[i:j] == str1[i:j][::-1]:
                    if j-i > max_len:
                        max_len = j-i
                        result = str1[i:j]
        return result

Guess you like

Origin www.cnblogs.com/Hijack-you/p/11913648.html