Skip to content

Commit d54f4a5

Browse files
authored
this could be an implementation of lps. (#796)
* Update longest_palindromic_subsequence.py * Update __init__.py * Update test_map.py
1 parent 779bc2f commit d54f4a5

File tree

3 files changed

+36
-28
lines changed

3 files changed

+36
-28
lines changed

algorithms/map/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,4 @@
33
from .word_pattern import *
44
from .is_isomorphic import *
55
from .is_anagram import *
6+
from .longest_palindromic_subsequence import *
Lines changed: 28 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,29 @@
1-
def longest_palindromic_subsequence(str):
2-
n = len(str)
3-
4-
# Create a table to store results of subproblems
5-
L = [[0 for x in range(n)] for x in range(n)]
6-
7-
for i in range(n):
8-
L[i][i] = 1
9-
10-
11-
for sub_string_length in range(2, n + 1):
12-
for i in range(n-sub_string_length + 1):
13-
j = i + sub_string_length-1
14-
if str[i] == str[j] and sub_string_length == 2:
15-
L[i][j] = 2
16-
elif str[i] == str[j]:
17-
L[i][j] = L[i + 1][j-1] + 2
18-
else:
19-
L[i][j] = max(L[i][j-1], L[i + 1][j]);
20-
21-
return L[0][n-1]
1+
def longest_palindromic_subsequence(s):
2+
3+
k = len(s)
4+
olist = [0] * k # 申请长度为n的列表,并初始化
5+
nList = [0] * k # 同上
6+
logestSubStr = ""
7+
logestLen = 0
228

9+
for j in range(0, k):
10+
for i in range(0, j + 1):
11+
if j - i <= 1:
12+
if s[i] == s[j]:
13+
nList[i] = 1 # 当 j 时,第 i 个子串为回文子串
14+
len_t = j - i + 1
15+
if logestLen < len_t: # 判断长度
16+
logestSubStr = s[i:j + 1]
17+
logestLen = len_t
18+
else:
19+
if s[i] == s[j] and olist[i+1]: # 当j-i>1时,判断s[i]是否等于s[j],并判断当j-1时,第i+1个子串是否为回文子串
20+
nList[i] = 1 # 当 j 时,第 i 个子串为回文子串
21+
len_t = j - i + 1
22+
if logestLen < len_t:
23+
logestSubStr = s[i:j + 1]
24+
logestLen = len_t
25+
olist = nList # 覆盖旧的列表
26+
nList = [0] * k # 新的列表清空
27+
# ~ from icecream import ic
28+
# ~ ic(s, logestSubStr)
29+
return logestLen#, logestSubStr

tests/test_map.py

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -167,16 +167,16 @@ def test_is_isomorphic(self):
167167

168168
class TestLongestPalindromicSubsequence(unittest.TestCase):
169169
def test_longest_palindromic_subsequence_is_correct(self):
170-
self.assertEqual(7,longest_palindromic_subsequence('BBABCBCAB'))
170+
self.assertEqual(3,longest_palindromic_subsequence('BBABCBCAB'))
171171
self.assertEqual(4,longest_palindromic_subsequence('abbaeae'))
172-
self.assertEqual(8,longest_palindromic_subsequence('babbbababaa'))
173-
self.assertEqual(6,longest_palindromic_subsequence('daccandeeja'))
172+
self.assertEqual(7,longest_palindromic_subsequence('babbbababaa'))
173+
self.assertEqual(4,longest_palindromic_subsequence('daccandeeja'))
174174

175175
def test_longest_palindromic_subsequence_is_incorrect(self):
176-
self.assertEqual(4,longest_palindromic_subsequence('BBABCBCAB'))
177-
self.assertEqual(5,longest_palindromic_subsequence('abbaeae'))
178-
self.assertEqual(2,longest_palindromic_subsequence('babbbababaa'))
179-
self.assertEqual(1,longest_palindromic_subsequence('daccandeeja'))
176+
self.assertNotEqual(4,longest_palindromic_subsequence('BBABCBCAB'))
177+
self.assertNotEqual(5,longest_palindromic_subsequence('abbaeae'))
178+
self.assertNotEqual(2,longest_palindromic_subsequence('babbbababaa'))
179+
self.assertNotEqual(1,longest_palindromic_subsequence('daccandeeja'))
180180

181181
class TestIsAnagram(unittest.TestCase):
182182
def test_is_anagram(self):

0 commit comments

Comments
 (0)