Python Forum
Python code for Longest Common Subsequence
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Python code for Longest Common Subsequence
#1
I am looking for a Python code to find the longest common subsequence of two strings. I found a blog post that describes the problem and provides a solution in Java. I would like to implement the same solution in Python.

The blog post can be found here on longest common subsequence dynamic programming:

The Java code for the longest common subsequence problem is as follows:

python
def longestCommonSubsequence(X, Y):
    m = len(X)
    n = len(Y)
    dp = [[0 for i in range(n + 1)] for j in range(m + 1)]
    for i in range(m + 1):
        for j in range(n + 1):
            if i == 0 or j == 0:
                dp[i][j] = 0
            elif X[i - 1] == Y[j - 1]:
                dp[i][j] = dp[i - 1][j - 1] + 1
            else:
                dp[i][j] = max(dp[i - 1][j], dp[i][j - 1])
    return dp[m][n]
Can you help me with this?
deanhystad write Sep-11-2023, 04:21 PM:
Please post all code, output and errors (it it's entirety) between their respective tags. Refer to BBCode help topic on how to post. Use the "Preview Post" button to make sure the code is presented as you expect before hitting the "Post Reply/Thread" button.
Reply
#2
What exactly you need help with? There is python code implementation in the article you refer to.

Also, please use proper BBCode tags
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#3
Your code looks like a mash up of the bottom-up approach and the dynamic programming approach. Pick one.
Reply
#4
(Sep-11-2023, 12:00 PM)buran Wrote: What exactly you need help with? There is python code implementation in the article you refer to.

Also, please use proper BBCode tags

Okay
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Python implementation of Longest Common Substring problem Bolt 0 2,203 Sep-17-2023, 08:31 PM
Last Post: Bolt
  longest palindromic subsequence DP solution bug usercat123 9 5,237 Feb-05-2022, 06:00 AM
Last Post: deanhystad
  a function common to methods of a class Skaperen 7 5,137 Oct-04-2021, 07:07 PM
Last Post: Skaperen
  Find Common Elements in 2 list quest 4 4,429 Apr-14-2021, 03:57 PM
Last Post: quest
  Check common element in tuples PUP280 10 10,614 Nov-05-2020, 05:01 PM
Last Post: PUP280
  Longest sequence of repeating integers in a numpy array Cricri 5 9,630 Jun-08-2020, 06:48 AM
Last Post: Cricri
  Keep the longest chains Amniote 11 7,403 Jul-03-2019, 05:07 PM
Last Post: nilamo
  common code, def a function vs exec() a string Skaperen 7 5,930 May-27-2019, 10:13 AM
Last Post: heiner55
  Omit pronoun/common words when searching in Python fabkhush 1 3,721 Feb-19-2019, 09:12 PM
Last Post: nilamo
  Interpolating to a common grid - help needed!!! Lightning1800 0 2,810 May-25-2018, 08:05 PM
Last Post: Lightning1800

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020