We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
1 parent ee6be82 commit 28fc848Copy full SHA for 28fc848
1 file changed
LPS
@@ -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