Permalink
Cannot retrieve contributors at this time
Name already in use
A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
LeetCode/#longest-palindromic-substring
Go to fileThis commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
110 lines (91 sloc)
3.14 KB
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Given a string s, find the longest palindromic substring in s. You may assume that the maximum length of s is 1000. | |
Example 1: | |
Input: "babad" | |
Output: "bab" | |
Note: "aba" is also a valid answer. | |
Example 2: | |
Input: "cbbd" | |
Output: "bb" | |
My code: | |
import numpy as np | |
class Solution: | |
def __init__(self): | |
self.result = "" | |
def longestPalindrome(self, s): | |
""" | |
:type s: str | |
:rtype: str | |
""" | |
# LCS method | |
if len(s) <= 1: | |
return s | |
self.flag = np.zeros((len(s), len(s))) - 1 | |
#Init base case | |
for i in range(len(s)): | |
self.flag[i,i]=1 | |
self.str_list = [] | |
for i in range(len(s)): | |
tmp_list = [] | |
for j in range(len(s)): | |
if i!=j: | |
tmp_str = "" | |
else: | |
tmp_str=s[i] | |
tmp_list.append(tmp_str) | |
self.str_list.append(tmp_list) | |
tmp_Str, tmp_flag = self.Recursive(s, 0, len(s) - 1) | |
#print(self.flag) | |
#print(tmp_flag) | |
#print(tmp_Str) | |
if len(tmp_Str)==1: | |
return s[0] | |
return tmp_Str | |
def Recursive(self, s1, i, j): | |
if len(s1) == 1: | |
return s1, True | |
if i>j: | |
return "", 0 | |
if self.flag[i, j] != -1: | |
tmp_flag = self.flag[i, j] | |
tmp_list = self.str_list[i] | |
tmp_str = tmp_list[j] | |
return tmp_str, tmp_flag | |
if s1[i] == s1[j]: | |
#print("now index i %d index j %d now the result %c"%(i,j,s1[i])) | |
tmp_str, tmp_flag = self.Recursive(s1, i + 1, j - 1) | |
if tmp_flag==j-i-1: | |
self.flag[i, j] = tmp_flag+2 | |
tmp_str = s1[i]+tmp_str + s1[i] | |
tmp_list = self.str_list[i] | |
tmp_list[j] = tmp_str | |
else: | |
tmp_str1, tmp_flag1 = self.Recursive(s1, i + 1, j) | |
tmp_str2, tmp_flag2 = self.Recursive(s1, i, j - 1) | |
if tmp_flag1 > tmp_flag2: | |
#print("index i %d index j %d" % (i, j)) | |
tmp_list = self.str_list[i] | |
tmp_list[j] = tmp_str1 | |
self.flag[i, j] = tmp_flag1 | |
return tmp_str1, tmp_flag1 | |
else: | |
tmp_list = self.str_list[i] | |
tmp_list[j] = tmp_str2 | |
self.flag[i, j] = tmp_flag2 | |
return tmp_str2, tmp_flag2 | |
return tmp_str, self.flag[i,j] | |
else: | |
tmp_str1, tmp_flag1 = self.Recursive(s1, i + 1, j) | |
tmp_str2, tmp_flag2 = self.Recursive(s1, i, j - 1) | |
if tmp_flag1>tmp_flag2: | |
#print("index i %d index j %d" % (i, j)) | |
tmp_list = self.str_list[i] | |
tmp_list[j] = tmp_str1 | |
self.flag[i, j] = tmp_flag1 | |
return tmp_str1, tmp_flag1 | |
else: | |
tmp_list = self.str_list[i] | |
tmp_list[j] = tmp_str2 | |
self.flag[i, j] = tmp_flag2 | |
return tmp_str2, tmp_flag2 | |
Solution: | |
https://leetcode.com/problems/longest-palindromic-substring/ |