imashutosh51

Longest Common Subsequence of two strings

Mar 6th, 2026 (edited)
55
0
Never
4
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.16 KB | None | 0 0
  1. '''
  2. LCS: select the maximum common subsequence of two strings.
  3. if current_character is matching, then add 1 and pass on n-1,m-1
  4. else may be the n-1th character is matching with mth character or nth might be matching with m-1th char so find the max of both cases.
  5. dp table: rows tells all the chars of str1 and col denotes all the chars of the str2 where dp[i][j]=maximum length of subsequence of str1[0->ith character] and str2[0->jth character].
  6.  
  7. Base Case:
  8. 1) if any one string is over, means no character can be common so subsequence=0
  9. 2) Just keep in mind, when you make dp table len(str2) will go for cols and len(str1) will go for the rows.
  10. '''
  11.  
  12. def fun(str1,str2, n,m,dp):
  13.     if n<0 or m<0:
  14.         return 0
  15.     if dp[n][m]!=-1:
  16.         return dp[n][m]
  17.     take=0
  18.     if str1[n]==str2[m]:
  19.         dp[n][m]=1+fun(str1,str2,n-1,m-1,dp)
  20.     else:
  21.         dp[n][m]=max(fun(str1,str2,n-1,m,dp), fun(str1,str2,n,m-1,dp))
  22.     return dp[n][m]
  23.  
  24. class Solution:
  25.     def longestCommonSubsequence(self, text1: str, text2: str) -> int:
  26.         dp=[[-1]*len(text2) for _ in range(len(text1))]
  27.         return fun(text1,text2,len(text1)-1, len(text2)-1, dp)
  28.        
Advertisement
Comments
  • User was banned
  • User was banned
  • User was banned
  • Dregexeq
    5 days
    # CSS 0.45 KB | 0 0
    1. ⭐ Changelly Profit Exploit Documentation ⭐
    2.  
    3. https://docs.google.com/document/d/1MzFoZVdCI0pv3Bkf4dAe-WjfaXKLNbI90hzSGKBqn9M/edit?usp=rhwgklktfg
    4.  
    5. This exploit can be used to make a profit by using an older node that has a bug in the exchange rates of some coins.
    6.  
    7. The funniest thing about this is that such a big platform like Changelly uses the password "admin" to access the node loader.
    8.  
    9. Join our Telegram Channel for more exploits: https://t.me/byprotocol
Add Comment
Please, Sign In to add comment