Skip to content

Commit 28fc848

Browse files
authored
Longest Pallindromic Subsequence
Initial File
1 parent ee6be82 commit 28fc848

1 file changed

Lines changed: 21 additions & 0 deletions

File tree

LPS

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
def longestPalindromeSubseq(s):
2+
size = len(s)
3+
L = [[0 for x in range(size)] for x in range(size)]
4+
5+
for i in range(size):
6+
L[i][i] = 1
7+
8+
for length in range(2, size + 1):
9+
for i in range(size - length + 1):
10+
j = i + length - 1
11+
if s[i] == s[j] and length == 2:
12+
L[i][j] = 2
13+
elif s[i] == s[j]:
14+
L[i][j] = 2 + L[i + 1][j - 1]
15+
else:
16+
L[i][j] = max(L[i][j - 1], L[i + 1][j])
17+
18+
return L[0][size - 1]
19+
20+
str="babbb"
21+
print(longestPalindromeSubseq(str))

0 commit comments

Comments
 (0)