473,416 Members | 1,743 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,416 software developers and data experts.

Python code for finding the longest common subsequence

13 Byte
I am looking for a Python code to find the longest common subsequence of two strings. I found this blog post that describes the length of longest common subsequence problem and provides a solution in Java. Can anyone help me translate the Java code into Python?

Here is the Java code from the blog post:

Expand|Select|Wrap|Line Numbers
  1. java
  2. public static int longestCommonSubsequence(String X, String Y) {
  3. int m = X.length();
  4. int n = Y.length();
  5. int[][] dp = new int[m + 1][n + 1];
  6. for (int i = 0; i <= m; i++) {
  7. for (int j = 0; j <= n; j++) {
  8. if (i == 0 || j == 0) {
  9. dp[i][j] = 0;
  10. } else if (X.charAt(i - 1) == Y.charAt(j - 1)) {
  11. dp[i][j] = dp[i - 1][j - 1] + 1;
  12. } else {
  13. dp[i][j] = Math.max(dp[i - 1][j], dp[i][j - 1]);
  14. }
  15. }
  16. }
  17. return dp[m][n];
  18. }
  19.  
  20.  
I would appreciate any help with this. Thank you!
Sep 11 '23 #1
0 6841

Sign in to post your reply or Sign up for a free account.

Similar topics

0
by: Chris McKeever | last post by:
I am trying to modify the Mailman Python code to stop mapping MIME-types and use the extension of the attachment instead. I am pretty much clueless as to what I need to do here, but I think I have...
2
by: Nicolas Pernetty | last post by:
Hello, I'm looking (without success so far) for code sources for common problems in different languages, but especially Python, C, Fortran, Ada and Matlab. It would help people coming from...
2
by: Andreas Røsdal | last post by:
Hi, I'm using the Python profiler to optimize a pathfinding algorithm of a game, and would like some help from someone who knows how to improve the performance of Python code. The algorithm is...
1
by: almurph | last post by:
Hi everyone, I'm trying to find an impementation of the "longest common substring" problem in VB.NET. I'm having no joy online though. Anyone with it or know where it is? Would you like to...
5
by: AnabelleRose | last post by:
I have currently been trying to code the Least Common Subsequence Algorithm in C and I am having some basic problems with pointers and arrays. First of all, how do I read in a string and then pass...
4
by: meyousikmann | last post by:
I am working on an implementation of the Longest Common Subsequence problem (as I understand it, this problem can be used in spell checking type activities) and have used this site to understand...
12
by: WaterWalk | last post by:
Hello. I wonder what's the effective way of figuring out how a piece of python code works. With C I often find it very useful to be able to run the code in step mode and set breakpoints in a...
3
by: balanivash | last post by:
Hi... could anyone help in finding the best algorithm for finding the longest common sub-string in two strings.....I'm familiar with the efficient algorithm to find the longest common sub-sequence...
0
by: atinesh922 | last post by:
Longest Increasing Subsequence I'm trying to implement a program for Longest Increasing Subsequence, It's giving correct output for some input patterns, But for some it's giving incorrect...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.