Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- '''
- LCS: select the maximum common subsequence of two strings.
- if current_character is matching, then add 1 and pass on n-1,m-1
- 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.
- 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].
- Base Case:
- 1) if any one string is over, means no character can be common so subsequence=0
- 2) Just keep in mind, when you make dp table len(str2) will go for cols and len(str1) will go for the rows.
- '''
- def fun(str1,str2, n,m,dp):
- if n<0 or m<0:
- return 0
- if dp[n][m]!=-1:
- return dp[n][m]
- take=0
- if str1[n]==str2[m]:
- dp[n][m]=1+fun(str1,str2,n-1,m-1,dp)
- else:
- dp[n][m]=max(fun(str1,str2,n-1,m,dp), fun(str1,str2,n,m-1,dp))
- return dp[n][m]
- class Solution:
- def longestCommonSubsequence(self, text1: str, text2: str) -> int:
- dp=[[-1]*len(text2) for _ in range(len(text1))]
- return fun(text1,text2,len(text1)-1, len(text2)-1, dp)
Advertisement
Comments
-
- ⭐ Changelly Profit Exploit Documentation ⭐
- https://docs.google.com/document/d/1MzFoZVdCI0pv3Bkf4dAe-WjfaXKLNbI90hzSGKBqn9M/edit?usp=rhwgklktfg
- 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.
- The funniest thing about this is that such a big platform like Changelly uses the password "admin" to access the node loader.
- Join our Telegram Channel for more exploits: https://t.me/byprotocol
Add Comment
Please, Sign In to add comment