Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Haroon Activity #13

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 27 additions & 0 deletions Longest_palindrome_subsequence.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
#Dynamic Programming (Bottom up Approach)
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you please mention the time complexity for this solution.

class Solution(object):
def longestPalindromeSubseq(self, s):
n = len(s)

# Use a 1D array to store the subproblem results
store = [0] * n

for i in range(n - 1, -1, -1):
new_store = [0] * n
new_store[i] = 1

for j in range(i + 1, n):
if s[i] == s[j]:
new_store[j] = store[j - 1] + 2
else:
new_store[j] = max(new_store[j - 1], store[j])

store = new_store

# The length of the longest palindromic subsequence in the entire string
return store[n - 1]

# Example usage
s1 = "bbbab"
sol = Solution()
print(sol.longestPalindromeSubseq(s1))