Skip to content

Commit b7abb05

Browse files
authored
add cleaner solution
1 parent 898d1a4 commit b7abb05

File tree

1 file changed

+16
-0
lines changed

1 file changed

+16
-0
lines changed

Kangli/DP/longestPalindromicSubstring.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,19 @@
1+
#clean dp solution
2+
class Solution(object):
3+
def longestPalindrome(self, s):
4+
"""
5+
:type s: str
6+
:rtype: str
7+
"""
8+
max_len, res = 0, ""
9+
dp = [[False for x in range(len(s))] for y in range(len(s))]
10+
for i in range(len(s)-1, -1, -1):
11+
for j in range(i, len(s)):
12+
dp[i][j] = s[i] == s[j] and (j-i < 3 or dp[i+1][j-1])
13+
if dp[i][j] and j-i+1 > len(res):
14+
res = s[i:j+1]
15+
return res
16+
117
class Solution(object):
218
def longestPalindrome(self, s):
319
st = 0

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy