Datasets:

inputs
stringlengths
50
14k
targets
stringlengths
4
655k
Given two strings text1 and text2, return the length of their longest common subsequence. A subsequence of a string is a new string generated from the original string with some characters(can be none) deleted without changing the relative order of the remaining characters. (eg, "ace" is a subsequence of "abcde" while "aec" is not). A common subsequence of two strings is a subsequence that is common to both strings.   If there is no common subsequence, return 0.   Example 1: Input: text1 = "abcde", text2 = "ace" Output: 3 Explanation: The longest common subsequence is "ace" and its length is 3. Example 2: Input: text1 = "abc", text2 = "abc" Output: 3 Explanation: The longest common subsequence is "abc" and its length is 3. Example 3: Input: text1 = "abc", text2 = "def" Output: 0 Explanation: There is no such common subsequence, so the result is 0.   Constraints: 1 <= text1.length <= 1000 1 <= text2.length <= 1000 The input strings consist of lowercase English characters only.
class Solution: def longestCommonSubsequence(self, text1: str, text2: str) -> int: dp = [[0 for i in range(len(text1)+1)] for j in range(len(text2)+1)] for i in range(1,len(dp)): for j in range(1,len(dp[0])): # in case of letter out of boarder dp[i][j] = max(dp[i-1][j],dp[i][j-1]) if text2[i-1] == text1[j-1]: # one more letter to sub sequence dp[i][j] = dp[i-1][j-1] + 1 return dp[-1][-1]
Given two strings text1 and text2, return the length of their longest common subsequence. A subsequence of a string is a new string generated from the original string with some characters(can be none) deleted without changing the relative order of the remaining characters. (eg, "ace" is a subsequence of "abcde" while "aec" is not). A common subsequence of two strings is a subsequence that is common to both strings.   If there is no common subsequence, return 0.   Example 1: Input: text1 = "abcde", text2 = "ace" Output: 3 Explanation: The longest common subsequence is "ace" and its length is 3. Example 2: Input: text1 = "abc", text2 = "abc" Output: 3 Explanation: The longest common subsequence is "abc" and its length is 3. Example 3: Input: text1 = "abc", text2 = "def" Output: 0 Explanation: There is no such common subsequence, so the result is 0.   Constraints: 1 <= text1.length <= 1000 1 <= text2.length <= 1000 The input strings consist of lowercase English characters only.
class Solution: def recursiveLCS(self, t1, t2): if not t1 or not t2: return 0 else: if t1[0] == t2[0]: return 1 + self.recursiveLCS(t1[1:], t2[1:]) else: left = self.recursiveLCS(t1[1:], t2) right = self.recursiveLCS(t1, t2[1:]) return max(left, right) def recursiveLongestCommonSubsequence(self, text1: str, text2: str) -> int: return self.recursiveLCS(text1, text2) def longestCommonSubsequence(self, text1: str, text2: str) -> int: # maybe do checks here but can probably get passed it DP = [[0]*(len(text2)+1) for i in range(len(text1)+1)] # MAYBE DO BASE CASES TO PRACTICE for i in range(1, len(text1)+1): for j in range(1, len(text2)+1): if text1[i-1] == text2[j-1]: DP[i][j] = 1 + DP[i-1][j-1] else: left = DP[i-1][j] right = DP[i][j-1] DP[i][j] = max(left, right) for row in DP: print(row) return DP[len(text1)][len(text2)]
Given two strings text1 and text2, return the length of their longest common subsequence. A subsequence of a string is a new string generated from the original string with some characters(can be none) deleted without changing the relative order of the remaining characters. (eg, "ace" is a subsequence of "abcde" while "aec" is not). A common subsequence of two strings is a subsequence that is common to both strings.   If there is no common subsequence, return 0.   Example 1: Input: text1 = "abcde", text2 = "ace" Output: 3 Explanation: The longest common subsequence is "ace" and its length is 3. Example 2: Input: text1 = "abc", text2 = "abc" Output: 3 Explanation: The longest common subsequence is "abc" and its length is 3. Example 3: Input: text1 = "abc", text2 = "def" Output: 0 Explanation: There is no such common subsequence, so the result is 0.   Constraints: 1 <= text1.length <= 1000 1 <= text2.length <= 1000 The input strings consist of lowercase English characters only.
from collections import defaultdict class Solution: def longestCommonSubsequence(self, text1: str, text2: str) -> int: dp = defaultdict(int) for i in range(len(text1)): for j in range(len(text2)): if text1[i] == text2[j]: dp[(i, j)] = 1 + dp[(i-1, j-1)] else: dp[(i, j)] = max(dp[(i-1, j)], dp[(i, j-1)]) return dp[(len(text1)-1, len(text2)-1)]
Given two strings text1 and text2, return the length of their longest common subsequence. A subsequence of a string is a new string generated from the original string with some characters(can be none) deleted without changing the relative order of the remaining characters. (eg, "ace" is a subsequence of "abcde" while "aec" is not). A common subsequence of two strings is a subsequence that is common to both strings.   If there is no common subsequence, return 0.   Example 1: Input: text1 = "abcde", text2 = "ace" Output: 3 Explanation: The longest common subsequence is "ace" and its length is 3. Example 2: Input: text1 = "abc", text2 = "abc" Output: 3 Explanation: The longest common subsequence is "abc" and its length is 3. Example 3: Input: text1 = "abc", text2 = "def" Output: 0 Explanation: There is no such common subsequence, so the result is 0.   Constraints: 1 <= text1.length <= 1000 1 <= text2.length <= 1000 The input strings consist of lowercase English characters only.
class Solution: def longestCommonSubsequence(self, text1: str, text2: str) -> int: str1, str2 = text1, text2 num_rows = len(str1) + 1 num_cols = len(str2) + 1 lengths = [[0 for _ in range(num_cols)] for _ in range(num_rows)] for row in range(1, num_rows): for col in range(1, num_cols): if str1[row - 1] == str2[col - 1]: lengths[row][col] = lengths[row - 1][col - 1] + 1 else: lengths[row][col] = max(lengths[row - 1][col], lengths[row][col - 1]) return lengths[-1][-1]
Given two strings text1 and text2, return the length of their longest common subsequence. A subsequence of a string is a new string generated from the original string with some characters(can be none) deleted without changing the relative order of the remaining characters. (eg, "ace" is a subsequence of "abcde" while "aec" is not). A common subsequence of two strings is a subsequence that is common to both strings.   If there is no common subsequence, return 0.   Example 1: Input: text1 = "abcde", text2 = "ace" Output: 3 Explanation: The longest common subsequence is "ace" and its length is 3. Example 2: Input: text1 = "abc", text2 = "abc" Output: 3 Explanation: The longest common subsequence is "abc" and its length is 3. Example 3: Input: text1 = "abc", text2 = "def" Output: 0 Explanation: There is no such common subsequence, so the result is 0.   Constraints: 1 <= text1.length <= 1000 1 <= text2.length <= 1000 The input strings consist of lowercase English characters only.
class Solution: def longestCommonSubsequence(self, text1: str, text2: str) -> int: m, n = len(text1), len(text2) dp = [[0] * (n + 1) for i in range(m + 1)] for i in range(m + 1): for j in range(n + 1): if i != 0 and j != 0: if text1[i - 1] == text2[j - 1]: dp[i][j] = dp[i - 1][j - 1] + 1 dp[i][j] = max(dp[i][j], max(dp[i - 1][j], dp[i][j - 1])) return dp[-1][-1]
Given two strings text1 and text2, return the length of their longest common subsequence. A subsequence of a string is a new string generated from the original string with some characters(can be none) deleted without changing the relative order of the remaining characters. (eg, "ace" is a subsequence of "abcde" while "aec" is not). A common subsequence of two strings is a subsequence that is common to both strings.   If there is no common subsequence, return 0.   Example 1: Input: text1 = "abcde", text2 = "ace" Output: 3 Explanation: The longest common subsequence is "ace" and its length is 3. Example 2: Input: text1 = "abc", text2 = "abc" Output: 3 Explanation: The longest common subsequence is "abc" and its length is 3. Example 3: Input: text1 = "abc", text2 = "def" Output: 0 Explanation: There is no such common subsequence, so the result is 0.   Constraints: 1 <= text1.length <= 1000 1 <= text2.length <= 1000 The input strings consist of lowercase English characters only.
import numpy as np #from functools import lru_cache class Solution: def longestCommonSubsequence(self, text1: str, text2: str) -> int: if len(text1) == 0 or len(text2) == 0: return 0 m = len(text1) n = len(text2) #return self.dp(text1, text2, m-1, n-1) A = np.zeros((m+1, n+1),int) for i in range(1,m+1): for j in range(1,n+1): if text1[i-1] == text2[j-1]: A[i,j] = A[i-1,j-1] +1 else: A[i,j] = max(A[i-1,j], A[i,j-1]) return A[-1,-1] # @lru_cache(maxsize=None) # def dp(self, text1, text2, i, j): # if i < 0 or j < 0: # return 0 # if text1[i] == text2[j]: # return self.dp(text1, text2, i-1, j-1) + 1 # else: # return max(self.dp(text1, text2, i-1, j), self.dp(text1, text2, i, j-1))
Given two strings text1 and text2, return the length of their longest common subsequence. A subsequence of a string is a new string generated from the original string with some characters(can be none) deleted without changing the relative order of the remaining characters. (eg, "ace" is a subsequence of "abcde" while "aec" is not). A common subsequence of two strings is a subsequence that is common to both strings.   If there is no common subsequence, return 0.   Example 1: Input: text1 = "abcde", text2 = "ace" Output: 3 Explanation: The longest common subsequence is "ace" and its length is 3. Example 2: Input: text1 = "abc", text2 = "abc" Output: 3 Explanation: The longest common subsequence is "abc" and its length is 3. Example 3: Input: text1 = "abc", text2 = "def" Output: 0 Explanation: There is no such common subsequence, so the result is 0.   Constraints: 1 <= text1.length <= 1000 1 <= text2.length <= 1000 The input strings consist of lowercase English characters only.
class Solution: def longestCommonSubsequence(self, text1: str, text2: str) -> int: dp = [[0]*(len(text2)+1) for i in range(len(text1)+1)] dp[0][0] = 0 for i in range(1, len(dp)): for j in range(1, len(dp[0])): if text1[i-1] == text2[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[len(text1)][len(text2)] # $ a c e # a # b # c # d # e
Given two strings text1 and text2, return the length of their longest common subsequence. A subsequence of a string is a new string generated from the original string with some characters(can be none) deleted without changing the relative order of the remaining characters. (eg, "ace" is a subsequence of "abcde" while "aec" is not). A common subsequence of two strings is a subsequence that is common to both strings.   If there is no common subsequence, return 0.   Example 1: Input: text1 = "abcde", text2 = "ace" Output: 3 Explanation: The longest common subsequence is "ace" and its length is 3. Example 2: Input: text1 = "abc", text2 = "abc" Output: 3 Explanation: The longest common subsequence is "abc" and its length is 3. Example 3: Input: text1 = "abc", text2 = "def" Output: 0 Explanation: There is no such common subsequence, so the result is 0.   Constraints: 1 <= text1.length <= 1000 1 <= text2.length <= 1000 The input strings consist of lowercase English characters only.
class Solution: def longestCommonSubsequence(self, text1: str, text2: str) -> int: m=len(text1) n=len(text2) t=[[0 for i in range(n+1)] for j in range(m+1)] for i in range(1,m+1): for j in range(1,n+1): if text1[i-1]==text2[j-1]: t[i][j]=1+t[i-1][j-1] else: t[i][j]=max(t[i][j-1],t[i-1][j]) return t[-1][-1]
Given two strings text1 and text2, return the length of their longest common subsequence. A subsequence of a string is a new string generated from the original string with some characters(can be none) deleted without changing the relative order of the remaining characters. (eg, "ace" is a subsequence of "abcde" while "aec" is not). A common subsequence of two strings is a subsequence that is common to both strings.   If there is no common subsequence, return 0.   Example 1: Input: text1 = "abcde", text2 = "ace" Output: 3 Explanation: The longest common subsequence is "ace" and its length is 3. Example 2: Input: text1 = "abc", text2 = "abc" Output: 3 Explanation: The longest common subsequence is "abc" and its length is 3. Example 3: Input: text1 = "abc", text2 = "def" Output: 0 Explanation: There is no such common subsequence, so the result is 0.   Constraints: 1 <= text1.length <= 1000 1 <= text2.length <= 1000 The input strings consist of lowercase English characters only.
class Solution: # approach 3 def longestCommonSubsequence(self, s1: str, s2: str) -> int: def print_grid(grid): for row in grid: print(row) dp_row = [0] * (len(s2) + 1) dp_row_pre = [0] * (len(s2) + 1) N, M = len(s1), len(s2) for i in range(N - 1, -1, -1): for j in range(M - 1, -1, -1): if s1[i] == s2[j]: dp_row[j] = dp_row_pre[j + 1] + 1 else: dp_row[j] = max(dp_row_pre[j], dp_row[j + 1]) dp_row_pre, dp_row = dp_row, dp_row_pre # print(\"update row=\", i, \", col=\", j) # print_grid(dp_grid) return dp_row_pre[0] # approach 1: recursive memorization # def longestCommonSubsequence(self, text1: str, text2: str) -> int: # @lru_cache(maxsize=None) # def memo_solve(p1, p2): # if p1 == len(text1) or p2 == len(text2): # return 0 # # option 1: we don't include text1[p1] in the solution # option_1 = memo_solve(p1 + 1, p2) # # option 2: we include p1 in the solution, as long as there is a match in p2 # first_occurrence = text2.find(text1[p1], p2) # option_2 = 0 # if first_occurrence != -1: # option_2 = 1 + memo_solve(p1 + 1, first_occurrence + 1) # return max(option_1, option_2) # return memo_solve(0, 0) # approach 2 # def longestCommonSubsequence(self, s1: str, s2: str) -> int: # @lru_cache(maxsize=None) # def mem_solve(p1: int, p2: int) -> int: # # base case # if p1 == len(s1) or p2 == len(s2): # return 0 # # option 1 match the first letter # if s1[p1] == s2[p2]: # return 1 + mem_solve(p1 + 1, p2 + 1) # # option 2 # return max(mem_solve(p1, p2 + 1), mem_solve(p1 + 1, p2)) # return mem_solve(0, 0)
Given two strings text1 and text2, return the length of their longest common subsequence. A subsequence of a string is a new string generated from the original string with some characters(can be none) deleted without changing the relative order of the remaining characters. (eg, "ace" is a subsequence of "abcde" while "aec" is not). A common subsequence of two strings is a subsequence that is common to both strings.   If there is no common subsequence, return 0.   Example 1: Input: text1 = "abcde", text2 = "ace" Output: 3 Explanation: The longest common subsequence is "ace" and its length is 3. Example 2: Input: text1 = "abc", text2 = "abc" Output: 3 Explanation: The longest common subsequence is "abc" and its length is 3. Example 3: Input: text1 = "abc", text2 = "def" Output: 0 Explanation: There is no such common subsequence, so the result is 0.   Constraints: 1 <= text1.length <= 1000 1 <= text2.length <= 1000 The input strings consist of lowercase English characters only.
from functools import lru_cache class Solution: def longestCommonSubsequence(self, text1: str, text2: str) -> int: l1 = len(text1) l2 = len(text2) memo = [[0 for _ in range(max(l1,l2)+1)] for _ in range(max(l1,l2)+1)] current=0 for i in range(1,l1+1): for j in range(1,l2+1): current = memo[i-1][j-1] if text1[i-1]==text2[j-1]: memo[i][j]=1+memo[i-1][j-1] else: memo[i][j]=max(memo[i-1][j],memo[i][j-1]) current=max(memo[i][j],current) return current
Given two strings text1 and text2, return the length of their longest common subsequence. A subsequence of a string is a new string generated from the original string with some characters(can be none) deleted without changing the relative order of the remaining characters. (eg, "ace" is a subsequence of "abcde" while "aec" is not). A common subsequence of two strings is a subsequence that is common to both strings.   If there is no common subsequence, return 0.   Example 1: Input: text1 = "abcde", text2 = "ace" Output: 3 Explanation: The longest common subsequence is "ace" and its length is 3. Example 2: Input: text1 = "abc", text2 = "abc" Output: 3 Explanation: The longest common subsequence is "abc" and its length is 3. Example 3: Input: text1 = "abc", text2 = "def" Output: 0 Explanation: There is no such common subsequence, so the result is 0.   Constraints: 1 <= text1.length <= 1000 1 <= text2.length <= 1000 The input strings consist of lowercase English characters only.
class Solution: def longestCommonSubsequence(self, text1: str, text2: str) -> int: m = len(text1) n = len(text2) dp = [[0 for i in range(n+1)] for j in range(m+1)] for i in range(1,m+1): for j in range(1,n+1): if text1[i-1]==text2[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]
Given two strings text1 and text2, return the length of their longest common subsequence. A subsequence of a string is a new string generated from the original string with some characters(can be none) deleted without changing the relative order of the remaining characters. (eg, "ace" is a subsequence of "abcde" while "aec" is not). A common subsequence of two strings is a subsequence that is common to both strings.   If there is no common subsequence, return 0.   Example 1: Input: text1 = "abcde", text2 = "ace" Output: 3 Explanation: The longest common subsequence is "ace" and its length is 3. Example 2: Input: text1 = "abc", text2 = "abc" Output: 3 Explanation: The longest common subsequence is "abc" and its length is 3. Example 3: Input: text1 = "abc", text2 = "def" Output: 0 Explanation: There is no such common subsequence, so the result is 0.   Constraints: 1 <= text1.length <= 1000 1 <= text2.length <= 1000 The input strings consist of lowercase English characters only.
import numpy as np class Solution: def longestCommonSubsequence(self, text1: str, text2: str) -> int: if len(text1) == 0 or len(text2) == 0: return 0 m = len(text1) n = len(text2) A = np.zeros((m+1, n+1),int) for i in range(1,m+1): for j in range(1,n+1): if text1[i-1] == text2[j-1]: A[i,j] = A[i-1,j-1] +1 else: A[i,j] = max(A[i-1,j], A[i,j-1]) return A[-1,-1]
Given two strings text1 and text2, return the length of their longest common subsequence. A subsequence of a string is a new string generated from the original string with some characters(can be none) deleted without changing the relative order of the remaining characters. (eg, "ace" is a subsequence of "abcde" while "aec" is not). A common subsequence of two strings is a subsequence that is common to both strings.   If there is no common subsequence, return 0.   Example 1: Input: text1 = "abcde", text2 = "ace" Output: 3 Explanation: The longest common subsequence is "ace" and its length is 3. Example 2: Input: text1 = "abc", text2 = "abc" Output: 3 Explanation: The longest common subsequence is "abc" and its length is 3. Example 3: Input: text1 = "abc", text2 = "def" Output: 0 Explanation: There is no such common subsequence, so the result is 0.   Constraints: 1 <= text1.length <= 1000 1 <= text2.length <= 1000 The input strings consist of lowercase English characters only.
class Solution: # def longestCommonSubsequence(self, text1: str, text2: str) -> int: # matrix = [[None]*(len(text2)+1) for i in range(len(text1)+1)] # for j in range(len(text2)+1): # matrix[0][j] = 0 # for i in range(len(text1)+1): # matrix[i][0] = 0 # for i in range(1, len(text1)+1): # for j in range(1, len(text2)+1): # if text1[i-1] == text2[j-1]: # matrix[i][j] = 1 + matrix[i-1][j-1] # else: # matrix[i][j] = max(matrix[i-1][j], matrix[i][j-1]) # return matrix[len(text1)][len(text2)] def longestCommonSubsequence(self, s1, s2): def _longest_common_subsequence(i, j, cache): if i < 0 or j < 0: return 0 elif cache[i][j] != None: return cache[i][j] elif s1[i] == s2[j]: answer = 1 + _longest_common_subsequence(i-1, j-1, cache) cache[i][j] = answer return answer else: answer = max(_longest_common_subsequence(i, j-1, cache), _longest_common_subsequence(i-1, j, cache)) cache[i][j] = answer return answer return _longest_common_subsequence(len(s1)-1, len(s2)-1, [[None]*len(s2) for i in range(len(s1))])
Given two strings text1 and text2, return the length of their longest common subsequence. A subsequence of a string is a new string generated from the original string with some characters(can be none) deleted without changing the relative order of the remaining characters. (eg, "ace" is a subsequence of "abcde" while "aec" is not). A common subsequence of two strings is a subsequence that is common to both strings.   If there is no common subsequence, return 0.   Example 1: Input: text1 = "abcde", text2 = "ace" Output: 3 Explanation: The longest common subsequence is "ace" and its length is 3. Example 2: Input: text1 = "abc", text2 = "abc" Output: 3 Explanation: The longest common subsequence is "abc" and its length is 3. Example 3: Input: text1 = "abc", text2 = "def" Output: 0 Explanation: There is no such common subsequence, so the result is 0.   Constraints: 1 <= text1.length <= 1000 1 <= text2.length <= 1000 The input strings consist of lowercase English characters only.
class Solution: def longestCommonSubsequence(self, text1: str, text2: str) -> int: l1, l2 = len(text1), len(text2) dp = [[0]*l2 for i in range(l1)] get = lambda r, c: 0 if -1 in [r, c] else dp[r][c] for r, c1 in enumerate(text1): # pos, char for c, c2 in enumerate(text2): if c1 == c2: dp[r][c] = get(r-1, c-1) + 1 else: dp[r][c] = max(get(r-1, c), get(r, c-1)) for i in dp: print(i) return dp[-1][-1]
Given two strings text1 and text2, return the length of their longest common subsequence. A subsequence of a string is a new string generated from the original string with some characters(can be none) deleted without changing the relative order of the remaining characters. (eg, "ace" is a subsequence of "abcde" while "aec" is not). A common subsequence of two strings is a subsequence that is common to both strings.   If there is no common subsequence, return 0.   Example 1: Input: text1 = "abcde", text2 = "ace" Output: 3 Explanation: The longest common subsequence is "ace" and its length is 3. Example 2: Input: text1 = "abc", text2 = "abc" Output: 3 Explanation: The longest common subsequence is "abc" and its length is 3. Example 3: Input: text1 = "abc", text2 = "def" Output: 0 Explanation: There is no such common subsequence, so the result is 0.   Constraints: 1 <= text1.length <= 1000 1 <= text2.length <= 1000 The input strings consist of lowercase English characters only.
class Solution: def longestCommonSubsequence(self, text1: str, text2: str) -> int: n = len(text1) m = len(text2) dp = [] for i in range(n): dp.append([-1]*m) def lcs(i,j): if(i>=n or j>=m): return 0 if(dp[i][j]!=-1): return dp[i][j] if(text1[i]==text2[j]): ans = 1 + lcs(i+1,j+1) else: ans = max(lcs(i+1,j),lcs(i,j+1)) dp[i][j] = ans return ans return lcs(0,0)
Given two strings text1 and text2, return the length of their longest common subsequence. A subsequence of a string is a new string generated from the original string with some characters(can be none) deleted without changing the relative order of the remaining characters. (eg, "ace" is a subsequence of "abcde" while "aec" is not). A common subsequence of two strings is a subsequence that is common to both strings.   If there is no common subsequence, return 0.   Example 1: Input: text1 = "abcde", text2 = "ace" Output: 3 Explanation: The longest common subsequence is "ace" and its length is 3. Example 2: Input: text1 = "abc", text2 = "abc" Output: 3 Explanation: The longest common subsequence is "abc" and its length is 3. Example 3: Input: text1 = "abc", text2 = "def" Output: 0 Explanation: There is no such common subsequence, so the result is 0.   Constraints: 1 <= text1.length <= 1000 1 <= text2.length <= 1000 The input strings consist of lowercase English characters only.
class Solution: def longestCommonSubsequence(self, text1, text2): def compute(x, y): if x == len(text1): return 0 if y == len(text2): return 0 if text1[x] == text2[y]: return 1 + compute(x+1, y+1) return max([compute(x+1, y), compute(x, y+1)]) cache = {} for x in range(-1, len(text1)+1): cache[(x, -1)] = 0 for y in range(-1, len(text2)+1): cache[(-1, y)] = 0 for x in range(len(text1)): for y in range(len(text2)): if text1[x] == text2[y]: cache[(x, y)] = 1 + cache[(x-1, y-1)] else: cache[(x, y)] = max([cache[(x, y-1)], cache[(x-1, y)]]) return cache[(len(text1)-1, len(text2)-1)]
Given two strings text1 and text2, return the length of their longest common subsequence. A subsequence of a string is a new string generated from the original string with some characters(can be none) deleted without changing the relative order of the remaining characters. (eg, "ace" is a subsequence of "abcde" while "aec" is not). A common subsequence of two strings is a subsequence that is common to both strings.   If there is no common subsequence, return 0.   Example 1: Input: text1 = "abcde", text2 = "ace" Output: 3 Explanation: The longest common subsequence is "ace" and its length is 3. Example 2: Input: text1 = "abc", text2 = "abc" Output: 3 Explanation: The longest common subsequence is "abc" and its length is 3. Example 3: Input: text1 = "abc", text2 = "def" Output: 0 Explanation: There is no such common subsequence, so the result is 0.   Constraints: 1 <= text1.length <= 1000 1 <= text2.length <= 1000 The input strings consist of lowercase English characters only.
class Solution: def longestCommonSubsequence(self, text1: str, text2: str) -> int: if len(text1) == 0 or len(text2) == 0: return 0 # grid = [[[None, 0, None, None] for i in range(len(text1) + 1)] for i in range(len(text2) + 1)] # for i in range(1, len(text2) + 1): # for j in range(1, len(text1) + 1): # if text1[j - 1] == text2[i - 1]: # grid[i][j] = [text2[i - 1], grid[i - 1][j - 1][1] + 1, i - 1, j - 1] # else: # if grid[i - 1][j][1] > grid[i][j - 1][1]: # grid[i][j] = [None, grid[i - 1][j][1], i - 1, j] # else: # grid[i][j] = [None, grid[i][j - 1][1], i, j - 1] # return grid[-1][-1][1] grid = [[[0] for i in range(len(text1) + 1)] for i in range(len(text2) + 1)] for i in range(1, len(text2) + 1): for j in range(1, len(text1) + 1): if text1[j - 1] == text2[i - 1]: grid[i][j] = [grid[i - 1][j - 1][0] + 1] else: if grid[i - 1][j] > grid[i][j - 1]: grid[i][j] = grid[i - 1][j] else: grid[i][j] = grid[i][j - 1] return grid[-1][-1][0]
Given two strings text1 and text2, return the length of their longest common subsequence. A subsequence of a string is a new string generated from the original string with some characters(can be none) deleted without changing the relative order of the remaining characters. (eg, "ace" is a subsequence of "abcde" while "aec" is not). A common subsequence of two strings is a subsequence that is common to both strings.   If there is no common subsequence, return 0.   Example 1: Input: text1 = "abcde", text2 = "ace" Output: 3 Explanation: The longest common subsequence is "ace" and its length is 3. Example 2: Input: text1 = "abc", text2 = "abc" Output: 3 Explanation: The longest common subsequence is "abc" and its length is 3. Example 3: Input: text1 = "abc", text2 = "def" Output: 0 Explanation: There is no such common subsequence, so the result is 0.   Constraints: 1 <= text1.length <= 1000 1 <= text2.length <= 1000 The input strings consist of lowercase English characters only.
class Solution: def longestCommonSubsequence(self, text1: str, text2: str) -> int: memo = [[-1] * (len(text2)+1) for _ in range(len(text1)+1)] return self.helper(text1, text2, 0, 0, memo) def helper(self, s1, s2, i, j, memo): if memo[i][j] != -1: return memo[i][j] if i == len(s1) or j == len(s2): return 0 ans = 0 if s1[i] == s2[j]: ans = 1 + self.helper(s1, s2, i+1, j+1, memo) else: ans = max(self.helper(s1, s2, i+1, j, memo), self.helper(s1, s2, i, j+1, memo)) memo[i][j] = ans return ans # def longestCommonSubsequence(self, text1: str, text2: str) -> int: # m, n = len(text1), len(text2) # dp = [[0] * (n+1) for _ in range(m+1)] # for i in range(1, m+1): # for j in range(1, n+1): # if text1[i-1] == text2[j-1]: # dp[i][j] = 1 + dp[i-1][j-1] # else: # dp[i][j] = max(dp[i-1][j], dp[i][j-1]) # return dp[-1][-1]
Given two strings text1 and text2, return the length of their longest common subsequence. A subsequence of a string is a new string generated from the original string with some characters(can be none) deleted without changing the relative order of the remaining characters. (eg, "ace" is a subsequence of "abcde" while "aec" is not). A common subsequence of two strings is a subsequence that is common to both strings.   If there is no common subsequence, return 0.   Example 1: Input: text1 = "abcde", text2 = "ace" Output: 3 Explanation: The longest common subsequence is "ace" and its length is 3. Example 2: Input: text1 = "abc", text2 = "abc" Output: 3 Explanation: The longest common subsequence is "abc" and its length is 3. Example 3: Input: text1 = "abc", text2 = "def" Output: 0 Explanation: There is no such common subsequence, so the result is 0.   Constraints: 1 <= text1.length <= 1000 1 <= text2.length <= 1000 The input strings consist of lowercase English characters only.
from functools import lru_cache class Solution: def longestCommonSubsequence(self, text1: str, text2: str) -> int: @lru_cache(None) def lcs(idxA, idxB): if idxA < 0 or idxB < 0: return 0 if text1[idxA] == text2[idxB]: return 1 + lcs(idxA - 1, idxB - 1) return max(lcs(idxA - 1, idxB), lcs(idxA, idxB - 1)) return lcs(len(text1) - 1, len(text2) - 1)
Given two strings text1 and text2, return the length of their longest common subsequence. A subsequence of a string is a new string generated from the original string with some characters(can be none) deleted without changing the relative order of the remaining characters. (eg, "ace" is a subsequence of "abcde" while "aec" is not). A common subsequence of two strings is a subsequence that is common to both strings.   If there is no common subsequence, return 0.   Example 1: Input: text1 = "abcde", text2 = "ace" Output: 3 Explanation: The longest common subsequence is "ace" and its length is 3. Example 2: Input: text1 = "abc", text2 = "abc" Output: 3 Explanation: The longest common subsequence is "abc" and its length is 3. Example 3: Input: text1 = "abc", text2 = "def" Output: 0 Explanation: There is no such common subsequence, so the result is 0.   Constraints: 1 <= text1.length <= 1000 1 <= text2.length <= 1000 The input strings consist of lowercase English characters only.
class Solution: def longestCommonSubsequence(self, text1: str, text2: str) -> int: # If text1 doesn't reference the shortest string, swap them. if len(text2) < len(text1): text1, text2 = text2, text1 # The previous column starts with all 0's and like before is 1 # more than the length of the first word. previous = [0] * (len(text1) + 1) # Create a new array to represent the current column. current = [0] * (len(text1) + 1) # Iterate up each column, starting from the last one. for col in reversed(range(len(text2))): for row in reversed(range(len(text1))): if text2[col] == text1[row]: current[row] = 1 + previous[row + 1] else: current[row] = max(previous[row], current[row + 1]) # The current column becomes the previous one. previous, current = current, previous # The original problem's answer is in previous[0]. Return it. return previous[0] ''' # Make a grid of 0's with len(text2) + 1 columns # and len(text1) + 1 rows. dp_grid = [[0] * (len(text2) + 1) for _ in range(len(text1) + 1)] # Iterate up each column, starting from the last one. for col in reversed(range(len(text2))): for row in reversed(range(len(text1))): # If the corresponding characters for this cell are the same... if text2[col] == text1[row]: dp_grid[row][col] = 1 + dp_grid[row + 1][col + 1] # Otherwise they must be different... else: dp_grid[row][col] = max(dp_grid[row + 1][col], dp_grid[row][col + 1]) # The original problem's answer is in dp_grid[0][0]. Return it. return dp_grid[0][0] ''' ''' from functools import lru_cache @lru_cache(maxsize=None) def memo_solve(p1, p2): # Base case: If either string is now empty, we can't match # up anymore characters. if p1 == len(text1) or p2 == len(text2): return 0 # Recursive case 1. if text1[p1] == text2[p2]: return 1 + memo_solve(p1 + 1, p2 + 1) # Recursive case 2. else: return max(memo_solve(p1, p2 + 1), memo_solve(p1 + 1, p2)) return memo_solve(0, 0) ''' ''' def memo_solve(p1, p2): # Base case: If either string is now empty, we can't match # up anymore characters. if p1 == len(text1) or p2 == len(text2): return 0 # Option 1: We don't include text1[p1] in the solution. option_1 = memo_solve(p1 + 1, p2) # Option 2: We include text1[p1] in the solution, as long as # a match for it in text2 at or after p2 exists. first_occurence = text2.find(text1[p1], p2) option_2 = 0 if first_occurence != -1: option_2 = 1 + memo_solve(p1 + 1, first_occurence + 1) # Return the best option. return max(option_1, option_2) return memo_solve(0, 0)'''
Given two strings text1 and text2, return the length of their longest common subsequence. A subsequence of a string is a new string generated from the original string with some characters(can be none) deleted without changing the relative order of the remaining characters. (eg, "ace" is a subsequence of "abcde" while "aec" is not). A common subsequence of two strings is a subsequence that is common to both strings.   If there is no common subsequence, return 0.   Example 1: Input: text1 = "abcde", text2 = "ace" Output: 3 Explanation: The longest common subsequence is "ace" and its length is 3. Example 2: Input: text1 = "abc", text2 = "abc" Output: 3 Explanation: The longest common subsequence is "abc" and its length is 3. Example 3: Input: text1 = "abc", text2 = "def" Output: 0 Explanation: There is no such common subsequence, so the result is 0.   Constraints: 1 <= text1.length <= 1000 1 <= text2.length <= 1000 The input strings consist of lowercase English characters only.
class Solution: def longestCommonSubsequence(self, text1: str, text2: str) -> int: rows = len(text1) cols = len(text2) dp = [[0 for j in range(cols)] for i in range(rows)] if text1[rows-1] == text2[cols-1]: dp[rows-1][cols-1] = 1 for i in range(rows-1, -1, -1): for j in range(cols-1, -1, -1): if i == rows - 1 and j == cols-1: continue right = 0 down = 0 rightdown = 0 if i < rows - 1: down = dp[i+1][j] if j < cols - 1: right = dp[i][j+1] if i < rows - 1 and j < cols - 1: rightdown = dp[i+1][j+1] if text1[i] == text2[j]: dp[i][j] = 1 + rightdown else: dp[i][j] = max(down, right) return dp[0][0]
Given two strings text1 and text2, return the length of their longest common subsequence. A subsequence of a string is a new string generated from the original string with some characters(can be none) deleted without changing the relative order of the remaining characters. (eg, "ace" is a subsequence of "abcde" while "aec" is not). A common subsequence of two strings is a subsequence that is common to both strings.   If there is no common subsequence, return 0.   Example 1: Input: text1 = "abcde", text2 = "ace" Output: 3 Explanation: The longest common subsequence is "ace" and its length is 3. Example 2: Input: text1 = "abc", text2 = "abc" Output: 3 Explanation: The longest common subsequence is "abc" and its length is 3. Example 3: Input: text1 = "abc", text2 = "def" Output: 0 Explanation: There is no such common subsequence, so the result is 0.   Constraints: 1 <= text1.length <= 1000 1 <= text2.length <= 1000 The input strings consist of lowercase English characters only.
class Solution: def longestCommonSubsequence(self, a: str, b: str) -> int: n1, n2 = len(a), len(b) memo = [[None] * (n2+1) for i in range(n1+1)] def helper(i, j): if memo[i][j] == None: if i == n1 or j == n2: memo[i][j] = 0 elif a[i] == b[j]: return 1 + helper(i+1, j+1) else: memo[i][j] = max(helper(i, j+1), helper(i+1, j)) return memo[i][j] return helper(0, 0)
Given two strings text1 and text2, return the length of their longest common subsequence. A subsequence of a string is a new string generated from the original string with some characters(can be none) deleted without changing the relative order of the remaining characters. (eg, "ace" is a subsequence of "abcde" while "aec" is not). A common subsequence of two strings is a subsequence that is common to both strings.   If there is no common subsequence, return 0.   Example 1: Input: text1 = "abcde", text2 = "ace" Output: 3 Explanation: The longest common subsequence is "ace" and its length is 3. Example 2: Input: text1 = "abc", text2 = "abc" Output: 3 Explanation: The longest common subsequence is "abc" and its length is 3. Example 3: Input: text1 = "abc", text2 = "def" Output: 0 Explanation: There is no such common subsequence, so the result is 0.   Constraints: 1 <= text1.length <= 1000 1 <= text2.length <= 1000 The input strings consist of lowercase English characters only.
from collections import defaultdict class Solution: def longestCommonSubsequence(self, text1: str, text2: str) -> int: # dp = defaultdict(lambda: -1) @lru_cache(maxsize = None) def helper(n, m): if m < 0 or n < 0: return 0 # if dp[(m, n)] == -1: if text1[n] == text2[m]: length = 1 + helper(n-1, m-1) else: length = max(helper(n-1, m), helper(n, m-1)) # dp[(m, n)] = length return length return helper(len(text1)-1, len(text2)-1)
Given two strings text1 and text2, return the length of their longest common subsequence. A subsequence of a string is a new string generated from the original string with some characters(can be none) deleted without changing the relative order of the remaining characters. (eg, "ace" is a subsequence of "abcde" while "aec" is not). A common subsequence of two strings is a subsequence that is common to both strings.   If there is no common subsequence, return 0.   Example 1: Input: text1 = "abcde", text2 = "ace" Output: 3 Explanation: The longest common subsequence is "ace" and its length is 3. Example 2: Input: text1 = "abc", text2 = "abc" Output: 3 Explanation: The longest common subsequence is "abc" and its length is 3. Example 3: Input: text1 = "abc", text2 = "def" Output: 0 Explanation: There is no such common subsequence, so the result is 0.   Constraints: 1 <= text1.length <= 1000 1 <= text2.length <= 1000 The input strings consist of lowercase English characters only.
class Solution: def longestCommonSubsequence(self, text1: str, text2: str) -> int: import functools @functools.lru_cache(None) def helper(i,j): if i<0 or j<0: return 0 if text1[i]==text2[j]: return helper(i-1,j-1)+1 return max(helper(i-1,j),helper(i,j-1)) return helper(len(text1)-1,len(text2)-1)
Given two strings text1 and text2, return the length of their longest common subsequence. A subsequence of a string is a new string generated from the original string with some characters(can be none) deleted without changing the relative order of the remaining characters. (eg, "ace" is a subsequence of "abcde" while "aec" is not). A common subsequence of two strings is a subsequence that is common to both strings.   If there is no common subsequence, return 0.   Example 1: Input: text1 = "abcde", text2 = "ace" Output: 3 Explanation: The longest common subsequence is "ace" and its length is 3. Example 2: Input: text1 = "abc", text2 = "abc" Output: 3 Explanation: The longest common subsequence is "abc" and its length is 3. Example 3: Input: text1 = "abc", text2 = "def" Output: 0 Explanation: There is no such common subsequence, so the result is 0.   Constraints: 1 <= text1.length <= 1000 1 <= text2.length <= 1000 The input strings consist of lowercase English characters only.
class Solution: def longestCommonSubsequence(self, text1: str, text2: str) -> int: m, n = len(text1), len(text2) @lru_cache(None) def find(i, j): if i >= m or j >= n: return 0 if text1[i] == text2[j]: return 1+find(i+1, j+1) else: return max(find(i+1, j), find(i, j+1)) return find(0, 0)
Given two strings text1 and text2, return the length of their longest common subsequence. A subsequence of a string is a new string generated from the original string with some characters(can be none) deleted without changing the relative order of the remaining characters. (eg, "ace" is a subsequence of "abcde" while "aec" is not). A common subsequence of two strings is a subsequence that is common to both strings.   If there is no common subsequence, return 0.   Example 1: Input: text1 = "abcde", text2 = "ace" Output: 3 Explanation: The longest common subsequence is "ace" and its length is 3. Example 2: Input: text1 = "abc", text2 = "abc" Output: 3 Explanation: The longest common subsequence is "abc" and its length is 3. Example 3: Input: text1 = "abc", text2 = "def" Output: 0 Explanation: There is no such common subsequence, so the result is 0.   Constraints: 1 <= text1.length <= 1000 1 <= text2.length <= 1000 The input strings consist of lowercase English characters only.
class Solution: def longestCommonSubsequence(self, text1: str, text2: str) -> int: rows = len(text1) cols = len(text2) dp = [[0 for j in range(cols)] for i in range(rows)] if text1[rows-1] == text2[cols-1]: dp[rows-1][cols-1] = 1 for i in range(rows-1, -1, -1): for j in range(cols-1, -1, -1): if i == rows - 1 and j == cols-1: continue print1 = False # if i == 2: # print1 = True if print1: print(i, j) right = 0 down = 0 rightdown = 0 if i < rows - 1: down = dp[i+1][j] if j < cols - 1: right = dp[i][j+1] if i < rows - 1 and j < cols - 1: rightdown = dp[i+1][j+1] # print(text1[i]) # print(text2[j]) # print(down) # print(right) if text1[i] == text2[j]: dp[i][j] = 1 + rightdown else: dp[i][j] = max(down, right) print(dp) return dp[0][0]
Given two strings text1 and text2, return the length of their longest common subsequence. A subsequence of a string is a new string generated from the original string with some characters(can be none) deleted without changing the relative order of the remaining characters. (eg, "ace" is a subsequence of "abcde" while "aec" is not). A common subsequence of two strings is a subsequence that is common to both strings.   If there is no common subsequence, return 0.   Example 1: Input: text1 = "abcde", text2 = "ace" Output: 3 Explanation: The longest common subsequence is "ace" and its length is 3. Example 2: Input: text1 = "abc", text2 = "abc" Output: 3 Explanation: The longest common subsequence is "abc" and its length is 3. Example 3: Input: text1 = "abc", text2 = "def" Output: 0 Explanation: There is no such common subsequence, so the result is 0.   Constraints: 1 <= text1.length <= 1000 1 <= text2.length <= 1000 The input strings consist of lowercase English characters only.
from functools import lru_cache class Solution: def longestCommonSubsequence(self, text1: str, text2: str) -> int: @lru_cache(maxsize =None) def memo(l1,l2): if l1 == len(text1) or l2 == len(text2): return 0 if text1[l1]==text2[l2]: return 1+ memo(l1+1,l2+1) else: return max(memo(l1+1,l2),memo(l1,l2+1)) return memo(0,0)
Given two strings text1 and text2, return the length of their longest common subsequence. A subsequence of a string is a new string generated from the original string with some characters(can be none) deleted without changing the relative order of the remaining characters. (eg, "ace" is a subsequence of "abcde" while "aec" is not). A common subsequence of two strings is a subsequence that is common to both strings.   If there is no common subsequence, return 0.   Example 1: Input: text1 = "abcde", text2 = "ace" Output: 3 Explanation: The longest common subsequence is "ace" and its length is 3. Example 2: Input: text1 = "abc", text2 = "abc" Output: 3 Explanation: The longest common subsequence is "abc" and its length is 3. Example 3: Input: text1 = "abc", text2 = "def" Output: 0 Explanation: There is no such common subsequence, so the result is 0.   Constraints: 1 <= text1.length <= 1000 1 <= text2.length <= 1000 The input strings consist of lowercase English characters only.
class Solution: def longestCommonSubsequence(self, text1: str, text2: str) -> int: @lru_cache(None) def recur(i, j): if i == -1 or j == -1: return 0 if text1[i] == text2[j]: return recur(i-1, j-1) + 1 else: return max(recur(i-1,j), recur(i, j -1)) return recur(len(text1) - 1, len(text2) - 1)
Given two strings text1 and text2, return the length of their longest common subsequence. A subsequence of a string is a new string generated from the original string with some characters(can be none) deleted without changing the relative order of the remaining characters. (eg, "ace" is a subsequence of "abcde" while "aec" is not). A common subsequence of two strings is a subsequence that is common to both strings.   If there is no common subsequence, return 0.   Example 1: Input: text1 = "abcde", text2 = "ace" Output: 3 Explanation: The longest common subsequence is "ace" and its length is 3. Example 2: Input: text1 = "abc", text2 = "abc" Output: 3 Explanation: The longest common subsequence is "abc" and its length is 3. Example 3: Input: text1 = "abc", text2 = "def" Output: 0 Explanation: There is no such common subsequence, so the result is 0.   Constraints: 1 <= text1.length <= 1000 1 <= text2.length <= 1000 The input strings consist of lowercase English characters only.
class Solution: def longestCommonSubsequence(self, text1: str, text2: str) -> int: # If text1 doesn't reference the shortest string, swap them. if len(text2) < len(text1): text1, text2 = text2, text1 # The previous column starts with all 0's and like before is 1 # more than the length of the first word. previous = [0] * (len(text1) + 1) # Iterate up each column, starting from the last one. for col in reversed(range(len(text2))): # Create a new array to represent the current column. current = [0] * (len(text1) + 1) for row in reversed(range(len(text1))): if text2[col] == text1[row]: current[row] = 1 + previous[row + 1] else: current[row] = max(previous[row], current[row + 1]) # The current column becomes the previous one. previous = current # The original problem's answer is in previous[0]. Return it. return previous[0] ''' # Make a grid of 0's with len(text2) + 1 columns # and len(text1) + 1 rows. dp_grid = [[0] * (len(text2) + 1) for _ in range(len(text1) + 1)] # Iterate up each column, starting from the last one. for col in reversed(range(len(text2))): for row in reversed(range(len(text1))): # If the corresponding characters for this cell are the same... if text2[col] == text1[row]: dp_grid[row][col] = 1 + dp_grid[row + 1][col + 1] # Otherwise they must be different... else: dp_grid[row][col] = max(dp_grid[row + 1][col], dp_grid[row][col + 1]) # The original problem's answer is in dp_grid[0][0]. Return it. return dp_grid[0][0] ''' ''' from functools import lru_cache @lru_cache(maxsize=None) def memo_solve(p1, p2): # Base case: If either string is now empty, we can't match # up anymore characters. if p1 == len(text1) or p2 == len(text2): return 0 # Recursive case 1. if text1[p1] == text2[p2]: return 1 + memo_solve(p1 + 1, p2 + 1) # Recursive case 2. else: return max(memo_solve(p1, p2 + 1), memo_solve(p1 + 1, p2)) return memo_solve(0, 0) ''' ''' def memo_solve(p1, p2): # Base case: If either string is now empty, we can't match # up anymore characters. if p1 == len(text1) or p2 == len(text2): return 0 # Option 1: We don't include text1[p1] in the solution. option_1 = memo_solve(p1 + 1, p2) # Option 2: We include text1[p1] in the solution, as long as # a match for it in text2 at or after p2 exists. first_occurence = text2.find(text1[p1], p2) option_2 = 0 if first_occurence != -1: option_2 = 1 + memo_solve(p1 + 1, first_occurence + 1) # Return the best option. return max(option_1, option_2) return memo_solve(0, 0)'''
Given two strings text1 and text2, return the length of their longest common subsequence. A subsequence of a string is a new string generated from the original string with some characters(can be none) deleted without changing the relative order of the remaining characters. (eg, "ace" is a subsequence of "abcde" while "aec" is not). A common subsequence of two strings is a subsequence that is common to both strings.   If there is no common subsequence, return 0.   Example 1: Input: text1 = "abcde", text2 = "ace" Output: 3 Explanation: The longest common subsequence is "ace" and its length is 3. Example 2: Input: text1 = "abc", text2 = "abc" Output: 3 Explanation: The longest common subsequence is "abc" and its length is 3. Example 3: Input: text1 = "abc", text2 = "def" Output: 0 Explanation: There is no such common subsequence, so the result is 0.   Constraints: 1 <= text1.length <= 1000 1 <= text2.length <= 1000 The input strings consist of lowercase English characters only.
from functools import lru_cache as memoize class Solution(): def longestCommonSubsequence(self, s1, s2): @memoize(None) def lcs(m,n): if(m==0 or n==0): return 0 if s1[m-1] == s2[n-1]: return 1 + lcs(m-1,n-1) else: return max(lcs(m-1,n),lcs(m,n-1)) return lcs(len(s1),len(s2))
Given two strings text1 and text2, return the length of their longest common subsequence. A subsequence of a string is a new string generated from the original string with some characters(can be none) deleted without changing the relative order of the remaining characters. (eg, "ace" is a subsequence of "abcde" while "aec" is not). A common subsequence of two strings is a subsequence that is common to both strings.   If there is no common subsequence, return 0.   Example 1: Input: text1 = "abcde", text2 = "ace" Output: 3 Explanation: The longest common subsequence is "ace" and its length is 3. Example 2: Input: text1 = "abc", text2 = "abc" Output: 3 Explanation: The longest common subsequence is "abc" and its length is 3. Example 3: Input: text1 = "abc", text2 = "def" Output: 0 Explanation: There is no such common subsequence, so the result is 0.   Constraints: 1 <= text1.length <= 1000 1 <= text2.length <= 1000 The input strings consist of lowercase English characters only.
class Solution: def longestCommonSubsequence(self, text1: str, text2: str) -> int: from functools import lru_cache @lru_cache(maxsize=None) def memo_solve(p1, p2): # Base case: If either string is now empty, we can't match # up anymore characters. if p1 == len(text1) or p2 == len(text2): return 0 # Recursive case 1. if text1[p1] == text2[p2]: return 1 + memo_solve(p1 + 1, p2 + 1) # Recursive case 2. else: return max(memo_solve(p1, p2 + 1), memo_solve(p1 + 1, p2)) return memo_solve(0, 0) ''' def memo_solve(p1, p2): # Base case: If either string is now empty, we can't match # up anymore characters. if p1 == len(text1) or p2 == len(text2): return 0 # Option 1: We don't include text1[p1] in the solution. option_1 = memo_solve(p1 + 1, p2) # Option 2: We include text1[p1] in the solution, as long as # a match for it in text2 at or after p2 exists. first_occurence = text2.find(text1[p1], p2) option_2 = 0 if first_occurence != -1: option_2 = 1 + memo_solve(p1 + 1, first_occurence + 1) # Return the best option. return max(option_1, option_2) return memo_solve(0, 0)'''
Given two strings text1 and text2, return the length of their longest common subsequence. A subsequence of a string is a new string generated from the original string with some characters(can be none) deleted without changing the relative order of the remaining characters. (eg, "ace" is a subsequence of "abcde" while "aec" is not). A common subsequence of two strings is a subsequence that is common to both strings.   If there is no common subsequence, return 0.   Example 1: Input: text1 = "abcde", text2 = "ace" Output: 3 Explanation: The longest common subsequence is "ace" and its length is 3. Example 2: Input: text1 = "abc", text2 = "abc" Output: 3 Explanation: The longest common subsequence is "abc" and its length is 3. Example 3: Input: text1 = "abc", text2 = "def" Output: 0 Explanation: There is no such common subsequence, so the result is 0.   Constraints: 1 <= text1.length <= 1000 1 <= text2.length <= 1000 The input strings consist of lowercase English characters only.
class Solution: def longestCommonSubsequence(self, text1: str, text2: str) -> int: def lcs(text1,text2,m,n,dp): if m==0 or n==0: return 0 if dp[m][n] !=-1: return dp[m][n] if text1[m-1] == text2[n-1]: dp[m][n] = 1 + lcs(text1,text2,m-1,n-1,dp) return dp[m][n] else: dp[m][n] = max(lcs(text1,text2,m,n-1,dp),lcs(text1,text2,m-1,n,dp)) return dp[m][n] m = len(text1) n = len(text2) dp = [[-1 for i in range(n+1)] for j in range(m+1)] res = lcs(text1,text2,m,n,dp) return res
Given two strings text1 and text2, return the length of their longest common subsequence. A subsequence of a string is a new string generated from the original string with some characters(can be none) deleted without changing the relative order of the remaining characters. (eg, "ace" is a subsequence of "abcde" while "aec" is not). A common subsequence of two strings is a subsequence that is common to both strings.   If there is no common subsequence, return 0.   Example 1: Input: text1 = "abcde", text2 = "ace" Output: 3 Explanation: The longest common subsequence is "ace" and its length is 3. Example 2: Input: text1 = "abc", text2 = "abc" Output: 3 Explanation: The longest common subsequence is "abc" and its length is 3. Example 3: Input: text1 = "abc", text2 = "def" Output: 0 Explanation: There is no such common subsequence, so the result is 0.   Constraints: 1 <= text1.length <= 1000 1 <= text2.length <= 1000 The input strings consist of lowercase English characters only.
from functools import lru_cache class Solution: def longestCommonSubsequence(self, text1: str, text2: str) -> int: @lru_cache(maxsize=None) def memo_solve(p1, p2): # Base case: If either string is now empty, we can't match # up anymore characters. if p1 == len(text1) or p2 == len(text2): return 0 # Recursive case 1. if text1[p1] == text2[p2]: return 1 + memo_solve(p1 + 1, p2 + 1) # Recursive case 2. else: return max(memo_solve(p1, p2 + 1), memo_solve(p1 + 1, p2)) return memo_solve(0, 0)
Given two strings text1 and text2, return the length of their longest common subsequence. A subsequence of a string is a new string generated from the original string with some characters(can be none) deleted without changing the relative order of the remaining characters. (eg, "ace" is a subsequence of "abcde" while "aec" is not). A common subsequence of two strings is a subsequence that is common to both strings.   If there is no common subsequence, return 0.   Example 1: Input: text1 = "abcde", text2 = "ace" Output: 3 Explanation: The longest common subsequence is "ace" and its length is 3. Example 2: Input: text1 = "abc", text2 = "abc" Output: 3 Explanation: The longest common subsequence is "abc" and its length is 3. Example 3: Input: text1 = "abc", text2 = "def" Output: 0 Explanation: There is no such common subsequence, so the result is 0.   Constraints: 1 <= text1.length <= 1000 1 <= text2.length <= 1000 The input strings consist of lowercase English characters only.
class Solution: def longestCommonSubsequence(self, text1: str, text2: str) -> int: @lru_cache(maxsize = None) def memo_solve(index1, index2): if index1 == len(text1) or index2 == len(text2): return 0 if text1[index1] == text2[index2]: return 1 + memo_solve(index1 + 1, index2 + 1) else: return max(memo_solve(index1, index2+1), memo_solve(index1+1, index2)) return memo_solve(0,0)
Given two strings text1 and text2, return the length of their longest common subsequence. A subsequence of a string is a new string generated from the original string with some characters(can be none) deleted without changing the relative order of the remaining characters. (eg, "ace" is a subsequence of "abcde" while "aec" is not). A common subsequence of two strings is a subsequence that is common to both strings.   If there is no common subsequence, return 0.   Example 1: Input: text1 = "abcde", text2 = "ace" Output: 3 Explanation: The longest common subsequence is "ace" and its length is 3. Example 2: Input: text1 = "abc", text2 = "abc" Output: 3 Explanation: The longest common subsequence is "abc" and its length is 3. Example 3: Input: text1 = "abc", text2 = "def" Output: 0 Explanation: There is no such common subsequence, so the result is 0.   Constraints: 1 <= text1.length <= 1000 1 <= text2.length <= 1000 The input strings consist of lowercase English characters only.
from functools import lru_cache class Solution: def longestCommonSubsequence(self, text1: str, text2: str) -> int: @lru_cache(maxsize=None) def memo(i, j): if i == len(text1) or j == len(text2): return 0 if text1[i] == text2[j]: return 1 + max(memo(i + 1, j + 1), memo(i + 1, j + 1)) else: return max(memo(i + 1, j), memo(i, j + 1)) return memo(0, 0)
Given two strings text1 and text2, return the length of their longest common subsequence. A subsequence of a string is a new string generated from the original string with some characters(can be none) deleted without changing the relative order of the remaining characters. (eg, "ace" is a subsequence of "abcde" while "aec" is not). A common subsequence of two strings is a subsequence that is common to both strings.   If there is no common subsequence, return 0.   Example 1: Input: text1 = "abcde", text2 = "ace" Output: 3 Explanation: The longest common subsequence is "ace" and its length is 3. Example 2: Input: text1 = "abc", text2 = "abc" Output: 3 Explanation: The longest common subsequence is "abc" and its length is 3. Example 3: Input: text1 = "abc", text2 = "def" Output: 0 Explanation: There is no such common subsequence, so the result is 0.   Constraints: 1 <= text1.length <= 1000 1 <= text2.length <= 1000 The input strings consist of lowercase English characters only.
class Solution: def longestCommonSubsequence(self, text1: str, text2: str) -> int: @lru_cache(maxsize=None) def helper(i, j): if i >= len(text1) or j >= len(text2): return 0 elif text1[i] == text2[j]: return 1 + helper(i+1, j+1) l = helper(i + 1, j) r = helper(i, j + 1) return max(l, r) return helper(0, 0)
Given two strings text1 and text2, return the length of their longest common subsequence. A subsequence of a string is a new string generated from the original string with some characters(can be none) deleted without changing the relative order of the remaining characters. (eg, "ace" is a subsequence of "abcde" while "aec" is not). A common subsequence of two strings is a subsequence that is common to both strings.   If there is no common subsequence, return 0.   Example 1: Input: text1 = "abcde", text2 = "ace" Output: 3 Explanation: The longest common subsequence is "ace" and its length is 3. Example 2: Input: text1 = "abc", text2 = "abc" Output: 3 Explanation: The longest common subsequence is "abc" and its length is 3. Example 3: Input: text1 = "abc", text2 = "def" Output: 0 Explanation: There is no such common subsequence, so the result is 0.   Constraints: 1 <= text1.length <= 1000 1 <= text2.length <= 1000 The input strings consist of lowercase English characters only.
from functools import lru_cache class Solution: def longestCommonSubsequence(self, text1: str, text2: str) -> int: @lru_cache(maxsize=None) def memo_solve(p1, p2): # Base case: If either string is now empty, we can't match # up anymore characters. if p1 == len(text1) or p2 == len(text2): return 0 if text1[p1] == text2[p2]: return 1 + memo_solve(p1+1, p2+1) else: return max(memo_solve(p1+1, p2), memo_solve(p1, p2+1)) return memo_solve(0, 0)
Given two strings text1 and text2, return the length of their longest common subsequence. A subsequence of a string is a new string generated from the original string with some characters(can be none) deleted without changing the relative order of the remaining characters. (eg, "ace" is a subsequence of "abcde" while "aec" is not). A common subsequence of two strings is a subsequence that is common to both strings.   If there is no common subsequence, return 0.   Example 1: Input: text1 = "abcde", text2 = "ace" Output: 3 Explanation: The longest common subsequence is "ace" and its length is 3. Example 2: Input: text1 = "abc", text2 = "abc" Output: 3 Explanation: The longest common subsequence is "abc" and its length is 3. Example 3: Input: text1 = "abc", text2 = "def" Output: 0 Explanation: There is no such common subsequence, so the result is 0.   Constraints: 1 <= text1.length <= 1000 1 <= text2.length <= 1000 The input strings consist of lowercase English characters only.
class Solution: def longestCommonSubsequence(self, text1: str, text2: str) -> int: prev = [0] * (len(text2) + 1) curr = [0] * (len(text2) + 1) for row in range(len(text1)): for col in range(len(text2)): if text1[row] == text2[col]: curr[col + 1] = 1 + prev[col] else: curr[col + 1] = max(prev[col + 1], curr[col]) prev, curr = curr, prev return prev[-1]
Given two strings text1 and text2, return the length of their longest common subsequence. A subsequence of a string is a new string generated from the original string with some characters(can be none) deleted without changing the relative order of the remaining characters. (eg, "ace" is a subsequence of "abcde" while "aec" is not). A common subsequence of two strings is a subsequence that is common to both strings.   If there is no common subsequence, return 0.   Example 1: Input: text1 = "abcde", text2 = "ace" Output: 3 Explanation: The longest common subsequence is "ace" and its length is 3. Example 2: Input: text1 = "abc", text2 = "abc" Output: 3 Explanation: The longest common subsequence is "abc" and its length is 3. Example 3: Input: text1 = "abc", text2 = "def" Output: 0 Explanation: There is no such common subsequence, so the result is 0.   Constraints: 1 <= text1.length <= 1000 1 <= text2.length <= 1000 The input strings consist of lowercase English characters only.
class Solution: def longestCommonSubsequence(self, text1: str, text2: str) -> int: # dp = [[0] * (len(text1) + 1) for _ in range(len(text2) + 1)] # # logic: if same, add 1, else max(try each without first) # # if row,col V == same, == prev diag + 1 # # else: = max(row-1,col, row,col-1) # for row in range(1, len(text2) + 1): # for col in range(1, len(text1) + 1): # if text2[row - 1] == text1[col - 1]: # dp[row][col] = dp[row-1][col-1] + 1 # else: # dp[row][col] = max(dp[row-1][col], dp[row][col-1]) # return dp[-1][-1] from functools import lru_cache @lru_cache(maxsize=None) def lcs(i, j): if i == len(text1) or j == len(text2): return 0 elif text1[i] == text2[j]: r = 1 + lcs(i + 1, j + 1) else: r = max(lcs(i + 1, j), lcs(i, j + 1)) return r return lcs(0, 0)
Given two strings text1 and text2, return the length of their longest common subsequence. A subsequence of a string is a new string generated from the original string with some characters(can be none) deleted without changing the relative order of the remaining characters. (eg, "ace" is a subsequence of "abcde" while "aec" is not). A common subsequence of two strings is a subsequence that is common to both strings.   If there is no common subsequence, return 0.   Example 1: Input: text1 = "abcde", text2 = "ace" Output: 3 Explanation: The longest common subsequence is "ace" and its length is 3. Example 2: Input: text1 = "abc", text2 = "abc" Output: 3 Explanation: The longest common subsequence is "abc" and its length is 3. Example 3: Input: text1 = "abc", text2 = "def" Output: 0 Explanation: There is no such common subsequence, so the result is 0.   Constraints: 1 <= text1.length <= 1000 1 <= text2.length <= 1000 The input strings consist of lowercase English characters only.
class Solution: def longestCommonSubsequence(self, s1: str, s2: str) -> int: @lru_cache(None) def helper(i, j): if i == len(s1) or j == len(s2): return 0 if s1[i] == s2[j]: return 1 + helper(i + 1, j + 1) else: return max(helper(i+1, j), helper(i, j + 1)) return helper(0,0)
Given two strings text1 and text2, return the length of their longest common subsequence. A subsequence of a string is a new string generated from the original string with some characters(can be none) deleted without changing the relative order of the remaining characters. (eg, "ace" is a subsequence of "abcde" while "aec" is not). A common subsequence of two strings is a subsequence that is common to both strings.   If there is no common subsequence, return 0.   Example 1: Input: text1 = "abcde", text2 = "ace" Output: 3 Explanation: The longest common subsequence is "ace" and its length is 3. Example 2: Input: text1 = "abc", text2 = "abc" Output: 3 Explanation: The longest common subsequence is "abc" and its length is 3. Example 3: Input: text1 = "abc", text2 = "def" Output: 0 Explanation: There is no such common subsequence, so the result is 0.   Constraints: 1 <= text1.length <= 1000 1 <= text2.length <= 1000 The input strings consist of lowercase English characters only.
class Solution: def longestCommonSubsequence(self, text1: str, text2: str) -> int: @lru_cache(maxsize=None) def memo_solve(p1, p2): # Base case: If either string is now empty, we can't match # up anymore characters. if p1 == len(text1) or p2 == len(text2): return 0 # Recursive case 1. if text1[p1] == text2[p2]: return 1 + memo_solve(p1 + 1, p2 + 1) # Recursive case 2. else: return max(memo_solve(p1, p2 + 1), memo_solve(p1 + 1, p2)) return memo_solve(0, 0)
Given two strings text1 and text2, return the length of their longest common subsequence. A subsequence of a string is a new string generated from the original string with some characters(can be none) deleted without changing the relative order of the remaining characters. (eg, "ace" is a subsequence of "abcde" while "aec" is not). A common subsequence of two strings is a subsequence that is common to both strings.   If there is no common subsequence, return 0.   Example 1: Input: text1 = "abcde", text2 = "ace" Output: 3 Explanation: The longest common subsequence is "ace" and its length is 3. Example 2: Input: text1 = "abc", text2 = "abc" Output: 3 Explanation: The longest common subsequence is "abc" and its length is 3. Example 3: Input: text1 = "abc", text2 = "def" Output: 0 Explanation: There is no such common subsequence, so the result is 0.   Constraints: 1 <= text1.length <= 1000 1 <= text2.length <= 1000 The input strings consist of lowercase English characters only.
class Solution: def longestCommonSubsequence(self, text1: str, text2: str) -> int: n = len(text1) m = len(text2) self.store = [[-1 for i in range(m)] for j in range(n)] self.Rec(text1,text2,n-1,m-1) return self.store[-1][-1] def Rec(self, text1, text2,n,m): if(n < 0 or m < 0): return 0 if(self.store[n][m] != -1): return self.store[n][m] if(text1[n] == text2[m]): x = 1+self.Rec(text1,text2,n-1,m-1) self.store[n][m] = x else: x = max(self.Rec(text1,text2,n-1,m), self.Rec(text1,text2,n,m-1)) self.store[n][m] = x return x
Given two strings text1 and text2, return the length of their longest common subsequence. A subsequence of a string is a new string generated from the original string with some characters(can be none) deleted without changing the relative order of the remaining characters. (eg, "ace" is a subsequence of "abcde" while "aec" is not). A common subsequence of two strings is a subsequence that is common to both strings.   If there is no common subsequence, return 0.   Example 1: Input: text1 = "abcde", text2 = "ace" Output: 3 Explanation: The longest common subsequence is "ace" and its length is 3. Example 2: Input: text1 = "abc", text2 = "abc" Output: 3 Explanation: The longest common subsequence is "abc" and its length is 3. Example 3: Input: text1 = "abc", text2 = "def" Output: 0 Explanation: There is no such common subsequence, so the result is 0.   Constraints: 1 <= text1.length <= 1000 1 <= text2.length <= 1000 The input strings consist of lowercase English characters only.
from functools import lru_cache class Solution: def longestCommonSubsequence(self, text1: str, text2: str) -> int: @lru_cache(maxsize=None) def memo_solve(p1, p2): # Base case: If either string is now empty, we can't match # up anymore characters. if p1 == len(text1) or p2 == len(text2): return 0 # Recursive case 1. if text1[p1] == text2[p2]: return 1 + memo_solve(p1 + 1, p2 + 1) # Recursive case 2. else: return max(memo_solve(p1, p2 + 1), memo_solve(p1 + 1, p2)) return memo_solve(0, 0)
Given two strings text1 and text2, return the length of their longest common subsequence. A subsequence of a string is a new string generated from the original string with some characters(can be none) deleted without changing the relative order of the remaining characters. (eg, "ace" is a subsequence of "abcde" while "aec" is not). A common subsequence of two strings is a subsequence that is common to both strings.   If there is no common subsequence, return 0.   Example 1: Input: text1 = "abcde", text2 = "ace" Output: 3 Explanation: The longest common subsequence is "ace" and its length is 3. Example 2: Input: text1 = "abc", text2 = "abc" Output: 3 Explanation: The longest common subsequence is "abc" and its length is 3. Example 3: Input: text1 = "abc", text2 = "def" Output: 0 Explanation: There is no such common subsequence, so the result is 0.   Constraints: 1 <= text1.length <= 1000 1 <= text2.length <= 1000 The input strings consist of lowercase English characters only.
class Solution: def longestCommonSubsequence(self, text1: str, text2: str) -> int: def lcs(i,j): if i==len(text1) or j==len(text2): return 0 if dp[i][j]!=None: return dp[i][j] if text1[i]==text2[j]: dp[i][j]=1+lcs(i+1,j+1) return dp[i][j] dp[i][j]=max(lcs(i+1,j),lcs(i,j+1)) return dp[i][j] dp=[[None for i in range(len(text2))] for j in range(len(text1))] # print(dp) # text1+=\"0\" # text2+=\"0\" lcs(0,0) return dp[0][0]
Given two strings text1 and text2, return the length of their longest common subsequence. A subsequence of a string is a new string generated from the original string with some characters(can be none) deleted without changing the relative order of the remaining characters. (eg, "ace" is a subsequence of "abcde" while "aec" is not). A common subsequence of two strings is a subsequence that is common to both strings.   If there is no common subsequence, return 0.   Example 1: Input: text1 = "abcde", text2 = "ace" Output: 3 Explanation: The longest common subsequence is "ace" and its length is 3. Example 2: Input: text1 = "abc", text2 = "abc" Output: 3 Explanation: The longest common subsequence is "abc" and its length is 3. Example 3: Input: text1 = "abc", text2 = "def" Output: 0 Explanation: There is no such common subsequence, so the result is 0.   Constraints: 1 <= text1.length <= 1000 1 <= text2.length <= 1000 The input strings consist of lowercase English characters only.
class Solution: def longestCommonSubsequence(self, text1: str, text2: str) -> int: # h = {} # def longest(t1, t2): # if (t1, t2) in h: # return h[(t1, t2)] # if not t1 or not t2: # h[(t1, t2)] = 0 # return h[(t1, t2)] # elif t1[0] == t2[0]: # h[(t1, t2)] = 1 + longest(t1[1:], t2[1:]) # return h[(t1, t2)] # else: # h[(t1, t2)] = max(longest(t1, t2[1:]), longest(t1[1:], t2)) # return h[(t1, t2)] # return longest(text1, text2) @lru_cache(None) def longest(i, j): if i == len(text1) or j == len(text2): return 0 elif text1[i] == text2[j]: return 1 + longest(i+1, j+1) else: return max(longest(i, j+1), longest(i+1, j)) return longest(0, 0)
Given two strings text1 and text2, return the length of their longest common subsequence. A subsequence of a string is a new string generated from the original string with some characters(can be none) deleted without changing the relative order of the remaining characters. (eg, "ace" is a subsequence of "abcde" while "aec" is not). A common subsequence of two strings is a subsequence that is common to both strings.   If there is no common subsequence, return 0.   Example 1: Input: text1 = "abcde", text2 = "ace" Output: 3 Explanation: The longest common subsequence is "ace" and its length is 3. Example 2: Input: text1 = "abc", text2 = "abc" Output: 3 Explanation: The longest common subsequence is "abc" and its length is 3. Example 3: Input: text1 = "abc", text2 = "def" Output: 0 Explanation: There is no such common subsequence, so the result is 0.   Constraints: 1 <= text1.length <= 1000 1 <= text2.length <= 1000 The input strings consist of lowercase English characters only.
from functools import lru_cache as memoize class Solution(): def longestCommonSubsequence(self, text1, text2): @memoize(None) def lcs(s1,s2,m,n): if(m==0 or n==0): return 0 if s1[m-1] == s2[n-1]: return 1 + lcs(s1,s2,m-1,n-1) else: return max(lcs(s1,s2,m-1,n),lcs(s1,s2,m,n-1)) return lcs(text1,text2,len(text1),len(text2))
Given two strings text1 and text2, return the length of their longest common subsequence. A subsequence of a string is a new string generated from the original string with some characters(can be none) deleted without changing the relative order of the remaining characters. (eg, "ace" is a subsequence of "abcde" while "aec" is not). A common subsequence of two strings is a subsequence that is common to both strings.   If there is no common subsequence, return 0.   Example 1: Input: text1 = "abcde", text2 = "ace" Output: 3 Explanation: The longest common subsequence is "ace" and its length is 3. Example 2: Input: text1 = "abc", text2 = "abc" Output: 3 Explanation: The longest common subsequence is "abc" and its length is 3. Example 3: Input: text1 = "abc", text2 = "def" Output: 0 Explanation: There is no such common subsequence, so the result is 0.   Constraints: 1 <= text1.length <= 1000 1 <= text2.length <= 1000 The input strings consist of lowercase English characters only.
class Solution: def longestCommonSubsequence(self, text1: str, text2: str) -> int: # if len(text2)>len(tex) memorize = [[-1] * (len(text2)+1) for _ in range(len(text1)+1)] def LCS(i1, i2): if i1 >= len(text1) or i2 >= len(text2): return 0 if memorize[i1][i2]!=-1: return memorize[i1][i2] if text1[i1] == text2[i2]: memorize[i1][i2] = 1 + LCS(i1+1, i2+1) else: memorize[i1][i2] = max(LCS(i1,i2+1), LCS(i1+1,i2)) return memorize[i1][i2] ans = LCS(0,0) return ans # def longestCommonSubsequence(self, text1: str, text2: str) -> int: # # if len(text2)>len(tex) # memorize = [[-1] * (len(text2)+1) for _ in range(len(text1)+1)] # def LCS(i1, i2): # if i1 >= len(text1) or i2 >= len(text2): return 0 # if memorize[i1+1][i2]==-1: # memorize[i1+1][i2] = LCS(i1+1, i2) # option1 = memorize[i1+1][i2] # first_occurence = text2.find(text1[i1], i2) # if first_occurence != -1: # if memorize[i1+1][i2+1]==-1: # memorize[i1+1][first_occurence+1] = LCS(i1+1, first_occurence+1) # option2 = 1 + memorize[i1+1][first_occurence+1] # else: option2 = 0 # return max(option1, option2) # ans = LCS(0,0) # return ans
Given two strings text1 and text2, return the length of their longest common subsequence. A subsequence of a string is a new string generated from the original string with some characters(can be none) deleted without changing the relative order of the remaining characters. (eg, "ace" is a subsequence of "abcde" while "aec" is not). A common subsequence of two strings is a subsequence that is common to both strings.   If there is no common subsequence, return 0.   Example 1: Input: text1 = "abcde", text2 = "ace" Output: 3 Explanation: The longest common subsequence is "ace" and its length is 3. Example 2: Input: text1 = "abc", text2 = "abc" Output: 3 Explanation: The longest common subsequence is "abc" and its length is 3. Example 3: Input: text1 = "abc", text2 = "def" Output: 0 Explanation: There is no such common subsequence, so the result is 0.   Constraints: 1 <= text1.length <= 1000 1 <= text2.length <= 1000 The input strings consist of lowercase English characters only.
from functools import lru_cache class Solution: def longestCommonSubsequence(self, text1: str, text2: str) -> int: @lru_cache(maxsize=None) def memo_solve(p1,p2): n = len(text1) m = len(text2) if n==p1 or m==p2: return 0 # case1 = memo_solve(p1+1,p2) # index = text2.find(text1[p1],p2) # if index !=-1: # case2 = 1+memo_solve(p1+1,index+1) # else: # case2 = 0 if text1[p1]==text2[p2]: return 1 + memo_solve(p1+1,p2+1) return max(memo_solve(p1+1,p2),memo_solve(p1,p2+1)) return memo_solve(0,0)
Given two strings text1 and text2, return the length of their longest common subsequence. A subsequence of a string is a new string generated from the original string with some characters(can be none) deleted without changing the relative order of the remaining characters. (eg, "ace" is a subsequence of "abcde" while "aec" is not). A common subsequence of two strings is a subsequence that is common to both strings.   If there is no common subsequence, return 0.   Example 1: Input: text1 = "abcde", text2 = "ace" Output: 3 Explanation: The longest common subsequence is "ace" and its length is 3. Example 2: Input: text1 = "abc", text2 = "abc" Output: 3 Explanation: The longest common subsequence is "abc" and its length is 3. Example 3: Input: text1 = "abc", text2 = "def" Output: 0 Explanation: There is no such common subsequence, so the result is 0.   Constraints: 1 <= text1.length <= 1000 1 <= text2.length <= 1000 The input strings consist of lowercase English characters only.
from typing import List class Solution: def longestCommonSubsequence(self, text1: str, text2: str) -> int: # dp[i][j] 表示 text1[0:i+1] 和 text2[0:j+1] 的 Longest Common Subsequence 的长度 # 其中 0 <= i < len(text1) # 其中 0 <= j < len(text2) dp: List[List[int]] = [] for i in range(len(text1)): dp.append(len(text2) * [-1]) def cal_with_cache(i: int, j: int): if i < 0 or j < 0: return 0 if dp[i][j] == -1: dp[i][j] = cal(i, j) return dp[i][j] def cal(i: int, j: int): if i == 0 and j == 0: return 1 if text1[i] == text2[j] else 0 if text1[i] == text2[j]: return cal_with_cache(i - 1, j - 1) + 1 else: return max(cal_with_cache(i - 1, j), cal_with_cache(i, j - 1)) return cal_with_cache(len(text1) - 1, len(text2) - 1)
Given two strings text1 and text2, return the length of their longest common subsequence. A subsequence of a string is a new string generated from the original string with some characters(can be none) deleted without changing the relative order of the remaining characters. (eg, "ace" is a subsequence of "abcde" while "aec" is not). A common subsequence of two strings is a subsequence that is common to both strings.   If there is no common subsequence, return 0.   Example 1: Input: text1 = "abcde", text2 = "ace" Output: 3 Explanation: The longest common subsequence is "ace" and its length is 3. Example 2: Input: text1 = "abc", text2 = "abc" Output: 3 Explanation: The longest common subsequence is "abc" and its length is 3. Example 3: Input: text1 = "abc", text2 = "def" Output: 0 Explanation: There is no such common subsequence, so the result is 0.   Constraints: 1 <= text1.length <= 1000 1 <= text2.length <= 1000 The input strings consist of lowercase English characters only.
class Solution: def longestCommonSubsequence(self, text1: str, text2: str) -> int: dp = defaultdict(int) for i, c1 in enumerate(text1): for j, c2 in enumerate(text2): dp[i,j] = max(dp[i-1, j-1] + (c1==c2), dp[i-1,j], dp[i,j-1]) return dp[len(text1)-1, len(text2)-1]
Given two strings text1 and text2, return the length of their longest common subsequence. A subsequence of a string is a new string generated from the original string with some characters(can be none) deleted without changing the relative order of the remaining characters. (eg, "ace" is a subsequence of "abcde" while "aec" is not). A common subsequence of two strings is a subsequence that is common to both strings.   If there is no common subsequence, return 0.   Example 1: Input: text1 = "abcde", text2 = "ace" Output: 3 Explanation: The longest common subsequence is "ace" and its length is 3. Example 2: Input: text1 = "abc", text2 = "abc" Output: 3 Explanation: The longest common subsequence is "abc" and its length is 3. Example 3: Input: text1 = "abc", text2 = "def" Output: 0 Explanation: There is no such common subsequence, so the result is 0.   Constraints: 1 <= text1.length <= 1000 1 <= text2.length <= 1000 The input strings consist of lowercase English characters only.
from functools import lru_cache class Solution: def longestCommonSubsequence(self, text1: str, text2: str) -> int: @lru_cache(None) def dp(i,j,strx,stry): if i>=len(strx) or j>=len(stry): return 0 elif strx[i]==stry[j]: return 1+dp(i+1,j+1,strx,stry) else: return max(dp(i,j+1,strx,stry),dp(i+1,j,strx,stry)) return dp(0,0,text1,text2)
Given two strings text1 and text2, return the length of their longest common subsequence. A subsequence of a string is a new string generated from the original string with some characters(can be none) deleted without changing the relative order of the remaining characters. (eg, "ace" is a subsequence of "abcde" while "aec" is not). A common subsequence of two strings is a subsequence that is common to both strings.   If there is no common subsequence, return 0.   Example 1: Input: text1 = "abcde", text2 = "ace" Output: 3 Explanation: The longest common subsequence is "ace" and its length is 3. Example 2: Input: text1 = "abc", text2 = "abc" Output: 3 Explanation: The longest common subsequence is "abc" and its length is 3. Example 3: Input: text1 = "abc", text2 = "def" Output: 0 Explanation: There is no such common subsequence, so the result is 0.   Constraints: 1 <= text1.length <= 1000 1 <= text2.length <= 1000 The input strings consist of lowercase English characters only.
class Solution: def longestCommonSubsequence(self, text1: str, text2: str) -> int: m, n = len(text1), len(text2) memo = [[-1 for _ in range(n)] for _ in range(m)] return self.lcs(text1, 0, text2, 0, memo) def lcs(self, t1, i, t2, j, memo): if i >= len(t1) or j >= len(t2): return 0 elif memo[i][j] != -1: return memo[i][j] else: result = -1 if t1[i] == t2[j]: result = 1 + self.lcs(t1, i+1, t2, j+1, memo) else: result = max(self.lcs(t1, i+1, t2, j, memo), self.lcs(t1, i, t2, j+1, memo)) memo[i][j] = result return result
Given two strings text1 and text2, return the length of their longest common subsequence. A subsequence of a string is a new string generated from the original string with some characters(can be none) deleted without changing the relative order of the remaining characters. (eg, "ace" is a subsequence of "abcde" while "aec" is not). A common subsequence of two strings is a subsequence that is common to both strings.   If there is no common subsequence, return 0.   Example 1: Input: text1 = "abcde", text2 = "ace" Output: 3 Explanation: The longest common subsequence is "ace" and its length is 3. Example 2: Input: text1 = "abc", text2 = "abc" Output: 3 Explanation: The longest common subsequence is "abc" and its length is 3. Example 3: Input: text1 = "abc", text2 = "def" Output: 0 Explanation: There is no such common subsequence, so the result is 0.   Constraints: 1 <= text1.length <= 1000 1 <= text2.length <= 1000 The input strings consist of lowercase English characters only.
class Solution: def longestCommonSubsequence(self, s1: str, s2: str) -> int: @lru_cache(None) def helper(s1, s2, i, j): if i == len(s1) or j == len(s2): return 0 if s1[i] == s2[j]: return 1 + helper(s1, s2, i + 1, j + 1) else: return max(helper(s1, s2, i+1, j), helper(s1, s2, i, j + 1)) return helper(s1,s2,0,0)
Given two strings text1 and text2, return the length of their longest common subsequence. A subsequence of a string is a new string generated from the original string with some characters(can be none) deleted without changing the relative order of the remaining characters. (eg, "ace" is a subsequence of "abcde" while "aec" is not). A common subsequence of two strings is a subsequence that is common to both strings.   If there is no common subsequence, return 0.   Example 1: Input: text1 = "abcde", text2 = "ace" Output: 3 Explanation: The longest common subsequence is "ace" and its length is 3. Example 2: Input: text1 = "abc", text2 = "abc" Output: 3 Explanation: The longest common subsequence is "abc" and its length is 3. Example 3: Input: text1 = "abc", text2 = "def" Output: 0 Explanation: There is no such common subsequence, so the result is 0.   Constraints: 1 <= text1.length <= 1000 1 <= text2.length <= 1000 The input strings consist of lowercase English characters only.
from functools import lru_cache class Solution: def longestCommonSubsequence(self, text1: str, text2: str) -> int: if len(text1) == 0 or len(text2) == 0: return 0 t1_idx = len(text1) - 1 t2_idx = len(text2) - 1 @lru_cache(maxsize=2147483647) def lcs(t1_idx, t2_idx): if t1_idx == -1 or t2_idx == -1: return 0 if text1[t1_idx] == text2[t2_idx]: return 1 + lcs(t1_idx - 1, t2_idx - 1) else: return max(lcs(t1_idx, t2_idx - 1), lcs(t1_idx - 1, t2_idx)) return lcs(t1_idx, t2_idx)
Given two strings text1 and text2, return the length of their longest common subsequence. A subsequence of a string is a new string generated from the original string with some characters(can be none) deleted without changing the relative order of the remaining characters. (eg, "ace" is a subsequence of "abcde" while "aec" is not). A common subsequence of two strings is a subsequence that is common to both strings.   If there is no common subsequence, return 0.   Example 1: Input: text1 = "abcde", text2 = "ace" Output: 3 Explanation: The longest common subsequence is "ace" and its length is 3. Example 2: Input: text1 = "abc", text2 = "abc" Output: 3 Explanation: The longest common subsequence is "abc" and its length is 3. Example 3: Input: text1 = "abc", text2 = "def" Output: 0 Explanation: There is no such common subsequence, so the result is 0.   Constraints: 1 <= text1.length <= 1000 1 <= text2.length <= 1000 The input strings consist of lowercase English characters only.
class Solution: # def longestCommonSubsequence(self, text1: str, text2: str) -> int: # matrix = [[None]*(len(text2)+1) for i in range(len(text1)+1)] # for j in range(len(text2)+1): # matrix[0][j] = 0 # for i in range(len(text1)+1): # matrix[i][0] = 0 # for i in range(1, len(text1)+1): # for j in range(1, len(text2)+1): # if text1[i-1] == text2[j-1]: # matrix[i][j] = 1 + matrix[i-1][j-1] # else: # matrix[i][j] = max(matrix[i-1][j], matrix[i][j-1]) # return matrix[len(text1)][len(text2)] def longestCommonSubsequence(self, s1, s2): def _longest_common_subsequence(i, j, cache): p = (i, j) if p in cache: return cache[p] elif i < 0 or j < 0: return 0 elif s1[i] == s2[j]: answer = 1 + _longest_common_subsequence(i-1, j-1, cache) cache[p] = answer return answer else: answer = max(_longest_common_subsequence(i, j-1, cache), _longest_common_subsequence(i-1, j, cache)) cache[p] = answer return answer return _longest_common_subsequence(len(s1)-1, len(s2)-1, {})
Given two strings text1 and text2, return the length of their longest common subsequence. A subsequence of a string is a new string generated from the original string with some characters(can be none) deleted without changing the relative order of the remaining characters. (eg, "ace" is a subsequence of "abcde" while "aec" is not). A common subsequence of two strings is a subsequence that is common to both strings.   If there is no common subsequence, return 0.   Example 1: Input: text1 = "abcde", text2 = "ace" Output: 3 Explanation: The longest common subsequence is "ace" and its length is 3. Example 2: Input: text1 = "abc", text2 = "abc" Output: 3 Explanation: The longest common subsequence is "abc" and its length is 3. Example 3: Input: text1 = "abc", text2 = "def" Output: 0 Explanation: There is no such common subsequence, so the result is 0.   Constraints: 1 <= text1.length <= 1000 1 <= text2.length <= 1000 The input strings consist of lowercase English characters only.
class Solution: def longestCommonSubsequence(self, text1: str, text2: str) -> int: memo = [[ -1 for i in range(len(text2))] for i in range (len(text1))] def memoSolve(i, j) : if i == len(text1) or j == len(text2): return 0 if memo[i][j] >= 0: return memo[i][j] answer = 0 if text1[i] == text2[j]: answer = 1+ memoSolve(i+1, j+1) else: answer = max(memoSolve(i+1,j), memoSolve(i,j+1)) memo[i][j] = answer return memo[i][j] return memoSolve(0,0)
Given two strings text1 and text2, return the length of their longest common subsequence. A subsequence of a string is a new string generated from the original string with some characters(can be none) deleted without changing the relative order of the remaining characters. (eg, "ace" is a subsequence of "abcde" while "aec" is not). A common subsequence of two strings is a subsequence that is common to both strings.   If there is no common subsequence, return 0.   Example 1: Input: text1 = "abcde", text2 = "ace" Output: 3 Explanation: The longest common subsequence is "ace" and its length is 3. Example 2: Input: text1 = "abc", text2 = "abc" Output: 3 Explanation: The longest common subsequence is "abc" and its length is 3. Example 3: Input: text1 = "abc", text2 = "def" Output: 0 Explanation: There is no such common subsequence, so the result is 0.   Constraints: 1 <= text1.length <= 1000 1 <= text2.length <= 1000 The input strings consist of lowercase English characters only.
class Solution: def longestCommonSubsequence(self, text1: str, text2: str) -> int: if not text1 or not text2: return 0 mem1 = [0] * (len(text2) + 1) mem2 = [0] * (len(text2) + 1) for i in range(len(text1)): for j in range(len(text2)): if text1[i] == text2[j]: mem2[j+1] = mem1[j] + 1 else: mem2[j+1] = max(mem2[j], mem1[j+1]) mem1 = mem2 mem2 = [0] * (len(text2) + 1) return mem1[-1]
Given two strings text1 and text2, return the length of their longest common subsequence. A subsequence of a string is a new string generated from the original string with some characters(can be none) deleted without changing the relative order of the remaining characters. (eg, "ace" is a subsequence of "abcde" while "aec" is not). A common subsequence of two strings is a subsequence that is common to both strings.   If there is no common subsequence, return 0.   Example 1: Input: text1 = "abcde", text2 = "ace" Output: 3 Explanation: The longest common subsequence is "ace" and its length is 3. Example 2: Input: text1 = "abc", text2 = "abc" Output: 3 Explanation: The longest common subsequence is "abc" and its length is 3. Example 3: Input: text1 = "abc", text2 = "def" Output: 0 Explanation: There is no such common subsequence, so the result is 0.   Constraints: 1 <= text1.length <= 1000 1 <= text2.length <= 1000 The input strings consist of lowercase English characters only.
class Solution: def __init__(self): self.dp = None def longestCommonSubsequence(self, text1: str, text2: str) -> int: self.dp = [[-1] * (len(text2) + 1) for _ in range(len(text1) + 1)] return self.lcs_rec(text1, text2, len(text1), len(text2)) def lcs_rec(self, x, y, n, m): # Base Condition if n == 0 or m == 0: return 0 # Memoization if self.dp[n][m] != -1: return self.dp[n][m] # Choice for subsequence if x[n - 1] == y[m - 1]: self.dp[n][m] = 1 + self.lcs_rec(x, y , n - 1, m - 1) else: self.dp[n][m] = max(self.lcs_rec(x, y, n - 1, m), self.lcs_rec(x, y, n, m - 1)) return self.dp[n][m]
Given two strings text1 and text2, return the length of their longest common subsequence. A subsequence of a string is a new string generated from the original string with some characters(can be none) deleted without changing the relative order of the remaining characters. (eg, "ace" is a subsequence of "abcde" while "aec" is not). A common subsequence of two strings is a subsequence that is common to both strings.   If there is no common subsequence, return 0.   Example 1: Input: text1 = "abcde", text2 = "ace" Output: 3 Explanation: The longest common subsequence is "ace" and its length is 3. Example 2: Input: text1 = "abc", text2 = "abc" Output: 3 Explanation: The longest common subsequence is "abc" and its length is 3. Example 3: Input: text1 = "abc", text2 = "def" Output: 0 Explanation: There is no such common subsequence, so the result is 0.   Constraints: 1 <= text1.length <= 1000 1 <= text2.length <= 1000 The input strings consist of lowercase English characters only.
class Solution: def longestCommonSubsequence(self, text1: str, text2: str) -> int: def s(i, j, cache): p = (i,j) if p not in cache: if text1[i] == text2[j]: cache[p] = 1 + s(i-1, j-1, cache) else: cache[p] = max(s(i, j-1, cache), s(i-1, j, cache)) return cache[p] cache = {(-1, i): 0 for i in range(len(text2))} for i in range(len(text1)): cache[(i, -1)] = 0 cache[(-1, -1)] = 0 return s(len(text1)-1, len(text2)-1, cache)
Given two strings text1 and text2, return the length of their longest common subsequence. A subsequence of a string is a new string generated from the original string with some characters(can be none) deleted without changing the relative order of the remaining characters. (eg, "ace" is a subsequence of "abcde" while "aec" is not). A common subsequence of two strings is a subsequence that is common to both strings.   If there is no common subsequence, return 0.   Example 1: Input: text1 = "abcde", text2 = "ace" Output: 3 Explanation: The longest common subsequence is "ace" and its length is 3. Example 2: Input: text1 = "abc", text2 = "abc" Output: 3 Explanation: The longest common subsequence is "abc" and its length is 3. Example 3: Input: text1 = "abc", text2 = "def" Output: 0 Explanation: There is no such common subsequence, so the result is 0.   Constraints: 1 <= text1.length <= 1000 1 <= text2.length <= 1000 The input strings consist of lowercase English characters only.
class Solution: def longestCommonSubsequence(self, text1: str, text2: str) -> int: @lru_cache(None) def dfs(i, j): if i < 0 or j < 0: return 0 if text1[i] == text2[j]: return 1+dfs(i-1, j-1) return max(dfs(i, j-1), dfs(i-1, j)) if not(text1 and text2): return 0 n, m = len(text1), len(text2) return dfs(n-1, m-1)
Given two strings text1 and text2, return the length of their longest common subsequence. A subsequence of a string is a new string generated from the original string with some characters(can be none) deleted without changing the relative order of the remaining characters. (eg, "ace" is a subsequence of "abcde" while "aec" is not). A common subsequence of two strings is a subsequence that is common to both strings.   If there is no common subsequence, return 0.   Example 1: Input: text1 = "abcde", text2 = "ace" Output: 3 Explanation: The longest common subsequence is "ace" and its length is 3. Example 2: Input: text1 = "abc", text2 = "abc" Output: 3 Explanation: The longest common subsequence is "abc" and its length is 3. Example 3: Input: text1 = "abc", text2 = "def" Output: 0 Explanation: There is no such common subsequence, so the result is 0.   Constraints: 1 <= text1.length <= 1000 1 <= text2.length <= 1000 The input strings consist of lowercase English characters only.
class Solution: def longestCommonSubsequence(self, text1: str, text2: str) -> int: dp = [[0 for i in range(len(text2))] for j in range(len(text1))] for i in range(len(text1)): for j in range(len(text2)): if text1[i] == text2[j]: dp[i][j] = (dp[i-1][j-1] if i>0 and j>0 else 0) + 1 else: p1 = dp[i-1][j] if i >0 else 0 p2 = dp[i][j-1] if j > 0 else 0 p3 = dp[i-1][j-1] if i>0 and j>0 else 0 max_num = max(p1,p2,p3) dp[i][j] = max_num return dp[-1][-1]
Given two strings text1 and text2, return the length of their longest common subsequence. A subsequence of a string is a new string generated from the original string with some characters(can be none) deleted without changing the relative order of the remaining characters. (eg, "ace" is a subsequence of "abcde" while "aec" is not). A common subsequence of two strings is a subsequence that is common to both strings.   If there is no common subsequence, return 0.   Example 1: Input: text1 = "abcde", text2 = "ace" Output: 3 Explanation: The longest common subsequence is "ace" and its length is 3. Example 2: Input: text1 = "abc", text2 = "abc" Output: 3 Explanation: The longest common subsequence is "abc" and its length is 3. Example 3: Input: text1 = "abc", text2 = "def" Output: 0 Explanation: There is no such common subsequence, so the result is 0.   Constraints: 1 <= text1.length <= 1000 1 <= text2.length <= 1000 The input strings consist of lowercase English characters only.
class Solution: def longestCommonSubsequence(self, text1: str, text2: str) -> int: if not text1 or not text2: return 0 cacheRow = [None] * len(text2) cache = [cacheRow[:] for i in range(len(text1))] return self.dp(text1, text2, 0, 0, cache) def dp(self, text1, text2, i, j, cache): if i >= len(text1) or j >= len(text2): return 0 if cache[i][j] is None: if text1[i] == text2[j]: cache[i][j] = 1 + self.dp(text1, text2, i+1, j+1, cache) else: cache[i][j] = max(self.dp(text1, text2, i+1, j, cache), self.dp(text1, text2, i, j+1, cache)) return cache[i][j]
Given two strings text1 and text2, return the length of their longest common subsequence. A subsequence of a string is a new string generated from the original string with some characters(can be none) deleted without changing the relative order of the remaining characters. (eg, "ace" is a subsequence of "abcde" while "aec" is not). A common subsequence of two strings is a subsequence that is common to both strings.   If there is no common subsequence, return 0.   Example 1: Input: text1 = "abcde", text2 = "ace" Output: 3 Explanation: The longest common subsequence is "ace" and its length is 3. Example 2: Input: text1 = "abc", text2 = "abc" Output: 3 Explanation: The longest common subsequence is "abc" and its length is 3. Example 3: Input: text1 = "abc", text2 = "def" Output: 0 Explanation: There is no such common subsequence, so the result is 0.   Constraints: 1 <= text1.length <= 1000 1 <= text2.length <= 1000 The input strings consist of lowercase English characters only.
class Solution: def longestCommonSubsequence(self, text1: str, text2: str) -> int: def s(i, j): p = (i,j) if p not in cache: if text1[i] == text2[j]: cache[p] = 1 + s(i-1, j-1) else: cache[p] = max(s(i, j-1), s(i-1, j)) return cache[p] cache = {(-1, i): 0 for i in range(len(text2))} for i in range(len(text1)): cache[(i, -1)] = 0 cache[(-1, -1)] = 0 return s(len(text1)-1, len(text2)-1)
Given two strings text1 and text2, return the length of their longest common subsequence. A subsequence of a string is a new string generated from the original string with some characters(can be none) deleted without changing the relative order of the remaining characters. (eg, "ace" is a subsequence of "abcde" while "aec" is not). A common subsequence of two strings is a subsequence that is common to both strings.   If there is no common subsequence, return 0.   Example 1: Input: text1 = "abcde", text2 = "ace" Output: 3 Explanation: The longest common subsequence is "ace" and its length is 3. Example 2: Input: text1 = "abc", text2 = "abc" Output: 3 Explanation: The longest common subsequence is "abc" and its length is 3. Example 3: Input: text1 = "abc", text2 = "def" Output: 0 Explanation: There is no such common subsequence, so the result is 0.   Constraints: 1 <= text1.length <= 1000 1 <= text2.length <= 1000 The input strings consist of lowercase English characters only.
class Solution: def longestCommonSubsequence(self, text1: str, text2: str) -> int: if not text1 or not text2: return 0 n1 = len(text1) n2 = len(text2) dp = [[0 for _ in range(n2 + 1)] for _ in range(2)] for i in range(n1 + 1): for j in range(n2 + 1): if i == 0 or j == 0: dp[i % 2][j] = 0 else: if text1[i - 1] == text2[j - 1]: dp[i % 2][j] = dp[1 - i % 2][j - 1] + 1 else: dp[i % 2][j] = max(dp[1 - i % 2][j], dp[i % 2][j - 1]) return dp[n1 % 2][n2]
Given two strings text1 and text2, return the length of their longest common subsequence. A subsequence of a string is a new string generated from the original string with some characters(can be none) deleted without changing the relative order of the remaining characters. (eg, "ace" is a subsequence of "abcde" while "aec" is not). A common subsequence of two strings is a subsequence that is common to both strings.   If there is no common subsequence, return 0.   Example 1: Input: text1 = "abcde", text2 = "ace" Output: 3 Explanation: The longest common subsequence is "ace" and its length is 3. Example 2: Input: text1 = "abc", text2 = "abc" Output: 3 Explanation: The longest common subsequence is "abc" and its length is 3. Example 3: Input: text1 = "abc", text2 = "def" Output: 0 Explanation: There is no such common subsequence, so the result is 0.   Constraints: 1 <= text1.length <= 1000 1 <= text2.length <= 1000 The input strings consist of lowercase English characters only.
class Solution: def longestCommonSubsequence(self, text1: str, text2: str) -> int: # Create dp 2D array dp = [[-1] * (len(text2)) for _ in range(len(text1))] # Recursive helper function def lcsForCell(i, j): # Boundary conditions if (i < 0) or (j < 0): return 0 if dp[i][j] != -1: return dp[i][j] # If letters are the same if text1[i] == text2[j]: dp[i][j] = lcsForCell(i-1, j-1) + 1 else: dp[i][j] = max(lcsForCell(i-1, j), lcsForCell(i, j-1)) return dp[i][j] return lcsForCell(len(text1)-1, len(text2)-1)
Given two strings text1 and text2, return the length of their longest common subsequence. A subsequence of a string is a new string generated from the original string with some characters(can be none) deleted without changing the relative order of the remaining characters. (eg, "ace" is a subsequence of "abcde" while "aec" is not). A common subsequence of two strings is a subsequence that is common to both strings.   If there is no common subsequence, return 0.   Example 1: Input: text1 = "abcde", text2 = "ace" Output: 3 Explanation: The longest common subsequence is "ace" and its length is 3. Example 2: Input: text1 = "abc", text2 = "abc" Output: 3 Explanation: The longest common subsequence is "abc" and its length is 3. Example 3: Input: text1 = "abc", text2 = "def" Output: 0 Explanation: There is no such common subsequence, so the result is 0.   Constraints: 1 <= text1.length <= 1000 1 <= text2.length <= 1000 The input strings consist of lowercase English characters only.
class Solution: def longestCommonSubsequence(self, text1: str, text2: str) -> int: if len(text1) < len(text2): text1, text2 = text2, text1 prev = [0] * (len(text2) + 1) curr = [0] * (len(text2) + 1) for row in range(len(text1)): for col in range(len(text2)): if text1[row] == text2[col]: curr[col + 1] = 1 + prev[col] else: curr[col + 1] = max(prev[col + 1], curr[col]) prev, curr = curr, prev return prev[-1]
Given two strings text1 and text2, return the length of their longest common subsequence. A subsequence of a string is a new string generated from the original string with some characters(can be none) deleted without changing the relative order of the remaining characters. (eg, "ace" is a subsequence of "abcde" while "aec" is not). A common subsequence of two strings is a subsequence that is common to both strings.   If there is no common subsequence, return 0.   Example 1: Input: text1 = "abcde", text2 = "ace" Output: 3 Explanation: The longest common subsequence is "ace" and its length is 3. Example 2: Input: text1 = "abc", text2 = "abc" Output: 3 Explanation: The longest common subsequence is "abc" and its length is 3. Example 3: Input: text1 = "abc", text2 = "def" Output: 0 Explanation: There is no such common subsequence, so the result is 0.   Constraints: 1 <= text1.length <= 1000 1 <= text2.length <= 1000 The input strings consist of lowercase English characters only.
''' The subproblem here is: suffixes \"abcde\" \"ce\" So becase first letter \"a\" is in both text, its 1 plus the lcs of \"bcde\" and \"ace\" if its not the same however, we try take suffix of both and see which one matches, we return longest ''' class Solution: def longestCommonSubsequence(self, text1: str, text2: str) -> int: n1 = len(text1) n2 = len(text2) memo = {} def lcs(pointer1, pointer2): if (pointer1, pointer2) in memo: return memo[(pointer1, pointer2)] if pointer1 >= n1 or pointer2 >= n2: return 0 if text1[pointer1] == text2[pointer2]: # move on to the next x = 1 + lcs(pointer1 + 1, pointer2 + 1) else: x = max(lcs(pointer1 + 1, pointer2), lcs(pointer1, pointer2+1)) memo[(pointer1, pointer2)] = x return x return lcs(0,0)
Given two strings text1 and text2, return the length of their longest common subsequence. A subsequence of a string is a new string generated from the original string with some characters(can be none) deleted without changing the relative order of the remaining characters. (eg, "ace" is a subsequence of "abcde" while "aec" is not). A common subsequence of two strings is a subsequence that is common to both strings.   If there is no common subsequence, return 0.   Example 1: Input: text1 = "abcde", text2 = "ace" Output: 3 Explanation: The longest common subsequence is "ace" and its length is 3. Example 2: Input: text1 = "abc", text2 = "abc" Output: 3 Explanation: The longest common subsequence is "abc" and its length is 3. Example 3: Input: text1 = "abc", text2 = "def" Output: 0 Explanation: There is no such common subsequence, so the result is 0.   Constraints: 1 <= text1.length <= 1000 1 <= text2.length <= 1000 The input strings consist of lowercase English characters only.
class Solution: def longestCommonSubsequence(self, text1: str, text2: str) -> int: ''' f[i][j]: 前i个,前j个匹配的最长 ''' m, n = len(text1), len(text2) f = [[0] * (n+1) for _ in range(m+1)] for i in range(m+1): for j in range(n+1): if i == 0 and j == 0: f[i][j] = 0 elif i == 0: f[i][j] = 0 elif j == 0: f[i][j] = 0 else: if text1[i-1] == text2[j-1]: f[i][j] = max(f[i][j-1], f[i-1][j], f[i-1][j-1] + 1) else: f[i][j] = max(f[i][j-1], f[i-1][j]) return f[m][n]
Given two strings text1 and text2, return the length of their longest common subsequence. A subsequence of a string is a new string generated from the original string with some characters(can be none) deleted without changing the relative order of the remaining characters. (eg, "ace" is a subsequence of "abcde" while "aec" is not). A common subsequence of two strings is a subsequence that is common to both strings.   If there is no common subsequence, return 0.   Example 1: Input: text1 = "abcde", text2 = "ace" Output: 3 Explanation: The longest common subsequence is "ace" and its length is 3. Example 2: Input: text1 = "abc", text2 = "abc" Output: 3 Explanation: The longest common subsequence is "abc" and its length is 3. Example 3: Input: text1 = "abc", text2 = "def" Output: 0 Explanation: There is no such common subsequence, so the result is 0.   Constraints: 1 <= text1.length <= 1000 1 <= text2.length <= 1000 The input strings consist of lowercase English characters only.
class Solution: def longestCommonSubsequence(self, text1: str, text2: str) -> int: def solve(i, j): # Base case: reached end of one of the strings if i == n or j == m: return 0 # Memoization case if memo[i][j] != -1: return memo[i][j] # Recursive case 1: text1[i] == text2[j] if text1[i] == text2[j]: memo[i][j] = 1 + solve(i+1, j+1) # Recursive case 2: text1[i] != text2[j] else: memo[i][j] = max(solve(i, j+1), solve(i+1, j)) return memo[i][j] n, m = len(text1), len(text2) memo = [[-1 for _ in range(m)] for _ in range(n)] return solve(0, 0)
Given two strings text1 and text2, return the length of their longest common subsequence. A subsequence of a string is a new string generated from the original string with some characters(can be none) deleted without changing the relative order of the remaining characters. (eg, "ace" is a subsequence of "abcde" while "aec" is not). A common subsequence of two strings is a subsequence that is common to both strings.   If there is no common subsequence, return 0.   Example 1: Input: text1 = "abcde", text2 = "ace" Output: 3 Explanation: The longest common subsequence is "ace" and its length is 3. Example 2: Input: text1 = "abc", text2 = "abc" Output: 3 Explanation: The longest common subsequence is "abc" and its length is 3. Example 3: Input: text1 = "abc", text2 = "def" Output: 0 Explanation: There is no such common subsequence, so the result is 0.   Constraints: 1 <= text1.length <= 1000 1 <= text2.length <= 1000 The input strings consist of lowercase English characters only.
class Solution: def longestCommonSubsequence(self, text1: str, text2: str) -> int: memo = [[-1] * (len(text2) + 1) for _ in range(len(text1) + 1)] def dfs(t1, t2): n1, n2 = len(t1), len(t2) if memo[n1][n2] != -1: return memo[n1][n2] if n1 == 0 or n2 == 0: return 0 if t1[-1] == t2[-1]: res = dfs(t1[:-1], t2[:-1]) + 1 else: res = max(dfs(t1[:-1], t2), dfs(t1, t2[:-1])) memo[n1][n2] = res return res dfs(text1, text2) return memo[-1][-1]
Given two strings text1 and text2, return the length of their longest common subsequence. A subsequence of a string is a new string generated from the original string with some characters(can be none) deleted without changing the relative order of the remaining characters. (eg, "ace" is a subsequence of "abcde" while "aec" is not). A common subsequence of two strings is a subsequence that is common to both strings.   If there is no common subsequence, return 0.   Example 1: Input: text1 = "abcde", text2 = "ace" Output: 3 Explanation: The longest common subsequence is "ace" and its length is 3. Example 2: Input: text1 = "abc", text2 = "abc" Output: 3 Explanation: The longest common subsequence is "abc" and its length is 3. Example 3: Input: text1 = "abc", text2 = "def" Output: 0 Explanation: There is no such common subsequence, so the result is 0.   Constraints: 1 <= text1.length <= 1000 1 <= text2.length <= 1000 The input strings consist of lowercase English characters only.
class Solution: def longestCommonSubsequence(self, text1: str, text2: str) -> int: def s(i, j, cache): p = (i,j) if p not in cache: c = 0 while i >= 0 and j >= 0 and text1[i] == text2[j]: c += 1 i -= 1 j -= 1 if c > 0: cache[p] = c + s(i, j, cache) else: cache[p] = max(s(i, j-1, cache), s(i-1, j, cache)) return cache[p] cache = {(-1, i): 0 for i in range(len(text2))} for i in range(len(text1)): cache[(i, -1)] = 0 cache[(-1, -1)] = 0 return s(len(text1)-1, len(text2)-1, cache)
Given two strings text1 and text2, return the length of their longest common subsequence. A subsequence of a string is a new string generated from the original string with some characters(can be none) deleted without changing the relative order of the remaining characters. (eg, "ace" is a subsequence of "abcde" while "aec" is not). A common subsequence of two strings is a subsequence that is common to both strings.   If there is no common subsequence, return 0.   Example 1: Input: text1 = "abcde", text2 = "ace" Output: 3 Explanation: The longest common subsequence is "ace" and its length is 3. Example 2: Input: text1 = "abc", text2 = "abc" Output: 3 Explanation: The longest common subsequence is "abc" and its length is 3. Example 3: Input: text1 = "abc", text2 = "def" Output: 0 Explanation: There is no such common subsequence, so the result is 0.   Constraints: 1 <= text1.length <= 1000 1 <= text2.length <= 1000 The input strings consist of lowercase English characters only.
class Solution: def longestCommonSubsequence(self, text1: str, text2: str) -> int: self.n = len(text1) self.m = len(text2) self.text1 = text1 self.text2 = text2 self.table = [] for i in range(self.n): self.table.append([-1] * self.m) return self.longest(0,0) def longest(self, i, j): if (i == self.n) or (j == self.m): return 0 if self.table[i][j] != -1: return self.table[i][j] else: if self.text1[i] == self.text2[j]: self.table[i][j] = 1 + self.longest(i+1, j+1) else: self.table[i][j] = max(self.longest(i+1, j), self.longest(i, j+1)) return self.table[i][j]
Given two strings text1 and text2, return the length of their longest common subsequence. A subsequence of a string is a new string generated from the original string with some characters(can be none) deleted without changing the relative order of the remaining characters. (eg, "ace" is a subsequence of "abcde" while "aec" is not). A common subsequence of two strings is a subsequence that is common to both strings.   If there is no common subsequence, return 0.   Example 1: Input: text1 = "abcde", text2 = "ace" Output: 3 Explanation: The longest common subsequence is "ace" and its length is 3. Example 2: Input: text1 = "abc", text2 = "abc" Output: 3 Explanation: The longest common subsequence is "abc" and its length is 3. Example 3: Input: text1 = "abc", text2 = "def" Output: 0 Explanation: There is no such common subsequence, so the result is 0.   Constraints: 1 <= text1.length <= 1000 1 <= text2.length <= 1000 The input strings consist of lowercase English characters only.
from functools import lru_cache class Solution: def longestCommonSubsequence(self, text1: str, text2: str) -> int: # @lru_cache(maxsize=None) # def recur(n,m): # if n == len(text1) or m == len(text2): # return 0 # if text1[n] == text2[m]: # return 1 + recur(n+1,m+1) # else: # return max(recur(n,m+1),recur(n+1,m)) # return recur(0,0) # '' a b c d e # '' 0 0 0 0 0 0 # a 0 1 1 1 1 1 # c 0 1 1 2 2 2 # e 0 1 1 2 2 3 # dp = [[0]*(len(text1)+1) for _ in range(len(text2)+1)] previous = [0] * (len(text1) + 1) current = [0] * (len(text1) + 1) # print(len(dp),len(dp[0])) for i in range(1,len(text2)+1): for j in range(1,len(text1)+1): if text2[i-1] == text1[j-1]: current[j] = previous[j-1] + 1 else: current[j] = max(current[j-1], previous[j]) previous = current current = [0] * (len(text1) + 1) return previous[-1] # dp_grid = [[0]*(len(text1)+1) for _ in range(len(text2)+1)] # for row in range(len(text2)): # for col in range(len(text1)): # # print(row,col) # if text1[col] == text2[row]: # dp_grid[row+1][col+1] = 1 + dp_grid[row][col] # else: # dp_grid[row+1][col+1] = max(dp_grid[row][col+1],dp_grid[row+1][col]) # return dp_grid[len(text2)][len(text1)]
Given two strings text1 and text2, return the length of their longest common subsequence. A subsequence of a string is a new string generated from the original string with some characters(can be none) deleted without changing the relative order of the remaining characters. (eg, "ace" is a subsequence of "abcde" while "aec" is not). A common subsequence of two strings is a subsequence that is common to both strings.   If there is no common subsequence, return 0.   Example 1: Input: text1 = "abcde", text2 = "ace" Output: 3 Explanation: The longest common subsequence is "ace" and its length is 3. Example 2: Input: text1 = "abc", text2 = "abc" Output: 3 Explanation: The longest common subsequence is "abc" and its length is 3. Example 3: Input: text1 = "abc", text2 = "def" Output: 0 Explanation: There is no such common subsequence, so the result is 0.   Constraints: 1 <= text1.length <= 1000 1 <= text2.length <= 1000 The input strings consist of lowercase English characters only.
class Solution: def longestCommonSubsequence(self, s1: str, s2: str) -> int: dp = [[0] * (len(s2) + 1) for _ in range(len(s1) + 1)] # s1: row # s2: col for s1_index in range(len(s1)): for s2_index in range(len(s2)): if s1[s1_index] != s2[s2_index]: dp[s1_index + 1][s2_index + 1] = max(dp[s1_index][s2_index + 1], dp[s1_index + 1][s2_index], dp[s1_index][s2_index]) else: dp[s1_index + 1][s2_index + 1] = max(dp[s1_index][s2_index + 1], dp[s1_index + 1][s2_index], dp[s1_index][s2_index] + 1) return dp[-1][-1]
Given two strings text1 and text2, return the length of their longest common subsequence. A subsequence of a string is a new string generated from the original string with some characters(can be none) deleted without changing the relative order of the remaining characters. (eg, "ace" is a subsequence of "abcde" while "aec" is not). A common subsequence of two strings is a subsequence that is common to both strings.   If there is no common subsequence, return 0.   Example 1: Input: text1 = "abcde", text2 = "ace" Output: 3 Explanation: The longest common subsequence is "ace" and its length is 3. Example 2: Input: text1 = "abc", text2 = "abc" Output: 3 Explanation: The longest common subsequence is "abc" and its length is 3. Example 3: Input: text1 = "abc", text2 = "def" Output: 0 Explanation: There is no such common subsequence, so the result is 0.   Constraints: 1 <= text1.length <= 1000 1 <= text2.length <= 1000 The input strings consist of lowercase English characters only.
class Solution: def longestCommonSubsequence(self, text1: str, text2: str) -> int: @lru_cache(maxsize=None) def memo_solve(p1, p2): # Base case: If either string is now empty, we can't match # up anymore characters. if p1 == len(text1) or p2 == len(text2): return 0 # Option 1: We don't include text1[p1] in the solution. option_1 = memo_solve(p1 + 1, p2) # Option 2: We include text1[p1] in the solution, as long as # a match for it in text2 at or after p2 exists. first_occurence = text2.find(text1[p1], p2) option_2 = 0 if first_occurence != -1: option_2 = 1 + memo_solve(p1 + 1, first_occurence + 1) # Return the best option. return max(option_1, option_2) return memo_solve(0, 0)
Given two strings text1 and text2, return the length of their longest common subsequence. A subsequence of a string is a new string generated from the original string with some characters(can be none) deleted without changing the relative order of the remaining characters. (eg, "ace" is a subsequence of "abcde" while "aec" is not). A common subsequence of two strings is a subsequence that is common to both strings.   If there is no common subsequence, return 0.   Example 1: Input: text1 = "abcde", text2 = "ace" Output: 3 Explanation: The longest common subsequence is "ace" and its length is 3. Example 2: Input: text1 = "abc", text2 = "abc" Output: 3 Explanation: The longest common subsequence is "abc" and its length is 3. Example 3: Input: text1 = "abc", text2 = "def" Output: 0 Explanation: There is no such common subsequence, so the result is 0.   Constraints: 1 <= text1.length <= 1000 1 <= text2.length <= 1000 The input strings consist of lowercase English characters only.
class Solution: def longestCommonSubsequence(self, text1: str, text2: str) -> int: def s(i, j, cache): if i < 0 or j < 0: return 0 p = (i,j) if p not in cache: if text1[i] == text2[j]: cache[p] = 1 + s(i-1, j-1, cache) else: cache[p] = max(s(i, j-1, cache), s(i-1, j, cache)) return cache[p] cache = {} # cache = {(-1, i): 0 for i in range(len(text2))} # for i in range(len(text1)): # cache[(i, -1)] = 0 # cache[(-1, -1)] = 0 return s(len(text1)-1, len(text2)-1, cache)
Given two strings text1 and text2, return the length of their longest common subsequence. A subsequence of a string is a new string generated from the original string with some characters(can be none) deleted without changing the relative order of the remaining characters. (eg, "ace" is a subsequence of "abcde" while "aec" is not). A common subsequence of two strings is a subsequence that is common to both strings.   If there is no common subsequence, return 0.   Example 1: Input: text1 = "abcde", text2 = "ace" Output: 3 Explanation: The longest common subsequence is "ace" and its length is 3. Example 2: Input: text1 = "abc", text2 = "abc" Output: 3 Explanation: The longest common subsequence is "abc" and its length is 3. Example 3: Input: text1 = "abc", text2 = "def" Output: 0 Explanation: There is no such common subsequence, so the result is 0.   Constraints: 1 <= text1.length <= 1000 1 <= text2.length <= 1000 The input strings consist of lowercase English characters only.
class Solution: def lcs(self, text1, text2, i1, i2, memo): if len(text1) == i1 or len(text2) == i2: return 0 if memo[i1][i2] != -1: return memo[i1][i2] if text1[i1] == text2[i2]: memo[i1][i2] = 1 + self.lcs(text1, text2, i1 + 1, i2 + 1, memo) return memo[i1][i2] maxLen = 0 maxLen = max(maxLen, self.lcs(text1, text2, i1 + 1, i2, memo)) maxLen = max(maxLen, self.lcs(text1, text2, i1, i2 + 1, memo)) memo[i1][i2] = maxLen return maxLen def longestCommonSubsequence(self, text1: str, text2: str) -> int: memo = [[-1 for _ in range(len(text2))] for _ in range(len(text1))] maxLen = self.lcs(text1, text2, 0, 0, memo) return maxLen
Given two strings text1 and text2, return the length of their longest common subsequence. A subsequence of a string is a new string generated from the original string with some characters(can be none) deleted without changing the relative order of the remaining characters. (eg, "ace" is a subsequence of "abcde" while "aec" is not). A common subsequence of two strings is a subsequence that is common to both strings.   If there is no common subsequence, return 0.   Example 1: Input: text1 = "abcde", text2 = "ace" Output: 3 Explanation: The longest common subsequence is "ace" and its length is 3. Example 2: Input: text1 = "abc", text2 = "abc" Output: 3 Explanation: The longest common subsequence is "abc" and its length is 3. Example 3: Input: text1 = "abc", text2 = "def" Output: 0 Explanation: There is no such common subsequence, so the result is 0.   Constraints: 1 <= text1.length <= 1000 1 <= text2.length <= 1000 The input strings consist of lowercase English characters only.
class Solution: def lcs(self, s1, s2, m, n): if m == 0 or n == 0: return 0 if self.dp[m][n] != -1: return self.dp[m][n] if s1[m - 1] == s2[n - 1]: self.dp[m][n] = 1 + self.lcs(s1, s2, m - 1, n - 1) else: self.dp[m][n] = max(self.lcs(s1, s2, m - 1, n), self.lcs(s1, s2, m, n - 1)) return self.dp[m][n] def longestCommonSubsequence(self, text1: str, text2: str) -> int: m = len(text1) n = len(text2) self.dp = [[-1 for _ in range(n + 1)] for __ in range(m + 1)] return self.lcs(text1, text2, m, n)
Given two strings text1 and text2, return the length of their longest common subsequence. A subsequence of a string is a new string generated from the original string with some characters(can be none) deleted without changing the relative order of the remaining characters. (eg, "ace" is a subsequence of "abcde" while "aec" is not). A common subsequence of two strings is a subsequence that is common to both strings.   If there is no common subsequence, return 0.   Example 1: Input: text1 = "abcde", text2 = "ace" Output: 3 Explanation: The longest common subsequence is "ace" and its length is 3. Example 2: Input: text1 = "abc", text2 = "abc" Output: 3 Explanation: The longest common subsequence is "abc" and its length is 3. Example 3: Input: text1 = "abc", text2 = "def" Output: 0 Explanation: There is no such common subsequence, so the result is 0.   Constraints: 1 <= text1.length <= 1000 1 <= text2.length <= 1000 The input strings consist of lowercase English characters only.
class Solution: def longestCommonSubsequence(self, text1: str, text2: str) -> int: mem = {} def dfs(l, r): if((l, r) in mem): return mem[(l, r)] if(l == len(text1) or r == len(text2)): return 0 ans = 0 if(text1[l] == text2[r]): ans = 1+dfs(l+1, r+1) else: ans = max(dfs(l+1, r), dfs(l, r+1)) mem[(l, r)] = ans return ans return dfs(0, 0)
Given two strings text1 and text2, return the length of their longest common subsequence. A subsequence of a string is a new string generated from the original string with some characters(can be none) deleted without changing the relative order of the remaining characters. (eg, "ace" is a subsequence of "abcde" while "aec" is not). A common subsequence of two strings is a subsequence that is common to both strings.   If there is no common subsequence, return 0.   Example 1: Input: text1 = "abcde", text2 = "ace" Output: 3 Explanation: The longest common subsequence is "ace" and its length is 3. Example 2: Input: text1 = "abc", text2 = "abc" Output: 3 Explanation: The longest common subsequence is "abc" and its length is 3. Example 3: Input: text1 = "abc", text2 = "def" Output: 0 Explanation: There is no such common subsequence, so the result is 0.   Constraints: 1 <= text1.length <= 1000 1 <= text2.length <= 1000 The input strings consist of lowercase English characters only.
''' text2 a c e 0 1 2 3 text1 0 0 0 0 0 a 1 0 1 1 1 b 2 0 1 1 1 c 3 0 1 2 2 d 4 0 1 2 2 e 5 0 1 2 3 ''' class Solution: def longestCommonSubsequence(self, text1: str, text2: str) -> int: # dp[i][j] reprents the length of the longest common subsequence for text1[:i] and text2[:j] m, n = len(text1) + 1, len(text2) + 1 dp = [[0] * n for _ in range(m)] res = 0 for i in range(1, m): # start from one char for j in range(1, n): if text1[i-1] == text2[j-1]: dp[i][j] = dp[i-1][j-1] + 1 else: dp[i][j] = max(dp[i][j-1], dp[i-1][j]) res = max(res, dp[i][j]) return res
Given two strings text1 and text2, return the length of their longest common subsequence. A subsequence of a string is a new string generated from the original string with some characters(can be none) deleted without changing the relative order of the remaining characters. (eg, "ace" is a subsequence of "abcde" while "aec" is not). A common subsequence of two strings is a subsequence that is common to both strings.   If there is no common subsequence, return 0.   Example 1: Input: text1 = "abcde", text2 = "ace" Output: 3 Explanation: The longest common subsequence is "ace" and its length is 3. Example 2: Input: text1 = "abc", text2 = "abc" Output: 3 Explanation: The longest common subsequence is "abc" and its length is 3. Example 3: Input: text1 = "abc", text2 = "def" Output: 0 Explanation: There is no such common subsequence, so the result is 0.   Constraints: 1 <= text1.length <= 1000 1 <= text2.length <= 1000 The input strings consist of lowercase English characters only.
class Solution: def longestCommonSubsequence(self, text1: str, text2: str) -> int: # dp = [[0] * (len(text1) + 1) for _ in range(len(text2) + 1)] # # logic: if same, add 1, else max(try each without first) # # if row,col V == same, == prev diag + 1 # # else: = max(row-1,col, row,col-1) # for row in range(1, len(text2) + 1): # for col in range(1, len(text1) + 1): # if text2[row - 1] == text1[col - 1]: # dp[row][col] = dp[row-1][col-1] + 1 # else: # dp[row][col] = max(dp[row-1][col], dp[row][col-1]) # return dp[-1][-1] from functools import lru_cache memo = {} def lcs(i, j): if (i, j) in memo: return memo[(i, j)] if i == len(text1) or j == len(text2): r = 0 elif text1[i] == text2[j]: r = 1 + lcs(i + 1, j + 1) else: r = max(lcs(i + 1, j), lcs(i, j + 1)) memo[(i, j)] = r return r return lcs(0, 0)
Given two strings text1 and text2, return the length of their longest common subsequence. A subsequence of a string is a new string generated from the original string with some characters(can be none) deleted without changing the relative order of the remaining characters. (eg, "ace" is a subsequence of "abcde" while "aec" is not). A common subsequence of two strings is a subsequence that is common to both strings.   If there is no common subsequence, return 0.   Example 1: Input: text1 = "abcde", text2 = "ace" Output: 3 Explanation: The longest common subsequence is "ace" and its length is 3. Example 2: Input: text1 = "abc", text2 = "abc" Output: 3 Explanation: The longest common subsequence is "abc" and its length is 3. Example 3: Input: text1 = "abc", text2 = "def" Output: 0 Explanation: There is no such common subsequence, so the result is 0.   Constraints: 1 <= text1.length <= 1000 1 <= text2.length <= 1000 The input strings consist of lowercase English characters only.
class Solution: def longestCommonSubsequence(self, text1: str, text2: str) -> int: if len(text1) <= len(text2) : ss, ls = text1, text2 else : ss, ls = text2, text1 track = [0] * len(ss) for i2 in range(0, len(ls)) : prev = track[0] if ls[i2] == ss[0] : track[0] = 1 for i1 in range(1, len(ss)) : newPrev = track[i1] if ls[i2] == ss[i1] : track[i1] = prev + 1 else : track[i1] = max(track[i1], track[i1-1]) prev = newPrev return track[-1]
Given two strings text1 and text2, return the length of their longest common subsequence. A subsequence of a string is a new string generated from the original string with some characters(can be none) deleted without changing the relative order of the remaining characters. (eg, "ace" is a subsequence of "abcde" while "aec" is not). A common subsequence of two strings is a subsequence that is common to both strings.   If there is no common subsequence, return 0.   Example 1: Input: text1 = "abcde", text2 = "ace" Output: 3 Explanation: The longest common subsequence is "ace" and its length is 3. Example 2: Input: text1 = "abc", text2 = "abc" Output: 3 Explanation: The longest common subsequence is "abc" and its length is 3. Example 3: Input: text1 = "abc", text2 = "def" Output: 0 Explanation: There is no such common subsequence, so the result is 0.   Constraints: 1 <= text1.length <= 1000 1 <= text2.length <= 1000 The input strings consist of lowercase English characters only.
class Solution: def longestCommonSubsequence(self, text1: str, text2: str) -> int: dp = {} def lcs(left, right): nonlocal text1 nonlocal text2 if (left, right) in dp: return dp[(left, right)] if left >= len(text1) or right >= len(text2): return 0 if text1[left] == text2[right]: count = 1 + lcs(left + 1, right + 1) dp[(left, right)] = count return count else: count = max(lcs(left + 1, right), lcs(left, right + 1)) dp[(left, right)] = count return count return lcs(0, 0)
Given two strings text1 and text2, return the length of their longest common subsequence. A subsequence of a string is a new string generated from the original string with some characters(can be none) deleted without changing the relative order of the remaining characters. (eg, "ace" is a subsequence of "abcde" while "aec" is not). A common subsequence of two strings is a subsequence that is common to both strings.   If there is no common subsequence, return 0.   Example 1: Input: text1 = "abcde", text2 = "ace" Output: 3 Explanation: The longest common subsequence is "ace" and its length is 3. Example 2: Input: text1 = "abc", text2 = "abc" Output: 3 Explanation: The longest common subsequence is "abc" and its length is 3. Example 3: Input: text1 = "abc", text2 = "def" Output: 0 Explanation: There is no such common subsequence, so the result is 0.   Constraints: 1 <= text1.length <= 1000 1 <= text2.length <= 1000 The input strings consist of lowercase English characters only.
class Solution: def longestCommonSubsequence(self, text1: str, text2: str) -> int: memo = {} def dpRecurse(i : int, j : int): result = 0 if (i, j) in memo: return memo[(i, j)] if i >= len(text1) or j >= len(text2): return 0; elif text1[i] == text2[j]: result = 1 + dpRecurse(i + 1, j + 1) else: result = max(dpRecurse(i + 1, j), dpRecurse(i, j + 1)) memo[(i, j)] = result return result return dpRecurse(0, 0)
Given two strings text1 and text2, return the length of their longest common subsequence. A subsequence of a string is a new string generated from the original string with some characters(can be none) deleted without changing the relative order of the remaining characters. (eg, "ace" is a subsequence of "abcde" while "aec" is not). A common subsequence of two strings is a subsequence that is common to both strings.   If there is no common subsequence, return 0.   Example 1: Input: text1 = "abcde", text2 = "ace" Output: 3 Explanation: The longest common subsequence is "ace" and its length is 3. Example 2: Input: text1 = "abc", text2 = "abc" Output: 3 Explanation: The longest common subsequence is "abc" and its length is 3. Example 3: Input: text1 = "abc", text2 = "def" Output: 0 Explanation: There is no such common subsequence, so the result is 0.   Constraints: 1 <= text1.length <= 1000 1 <= text2.length <= 1000 The input strings consist of lowercase English characters only.
class Solution: def longestCommonSubsequence(self, text1: str, text2: str) -> int: from functools import lru_cache @lru_cache(maxsize=None) def memo_solve(p1, p2): # Base case: If either string is now empty, we can't match # up anymore characters. if p1 == len(text1) or p2 == len(text2): return 0 # Option 1: We don't include text1[p1] in the solution. option_1 = memo_solve(p1 + 1, p2) # Option 2: We include text1[p1] in the solution, as long as # a match for it in text2 at or after p2 exists. first_occurence = text2.find(text1[p1], p2) option_2 = 0 if first_occurence != -1: option_2 = 1 + memo_solve(p1 + 1, first_occurence + 1) # Return the best option. return max(option_1, option_2) return memo_solve(0, 0)
Given two strings text1 and text2, return the length of their longest common subsequence. A subsequence of a string is a new string generated from the original string with some characters(can be none) deleted without changing the relative order of the remaining characters. (eg, "ace" is a subsequence of "abcde" while "aec" is not). A common subsequence of two strings is a subsequence that is common to both strings.   If there is no common subsequence, return 0.   Example 1: Input: text1 = "abcde", text2 = "ace" Output: 3 Explanation: The longest common subsequence is "ace" and its length is 3. Example 2: Input: text1 = "abc", text2 = "abc" Output: 3 Explanation: The longest common subsequence is "abc" and its length is 3. Example 3: Input: text1 = "abc", text2 = "def" Output: 0 Explanation: There is no such common subsequence, so the result is 0.   Constraints: 1 <= text1.length <= 1000 1 <= text2.length <= 1000 The input strings consist of lowercase English characters only.
from functools import lru_cache class Solution: def longestCommonSubsequence(self, text1: str, text2: str) -> int: @lru_cache(maxsize=None) def memo_solve(p1, p2): # Base case: If either string is now empty, we can't match # up anymore characters. if p1 == len(text1) or p2 == len(text2): return 0 # Option 1: We don't include text1[p1] in the solution. option_1 = memo_solve(p1 + 1, p2) # Option 2: We include text1[p1] in the solution, as long as # a match for it in text2 at or after p2 exists. first_occurence = text2.find(text1[p1], p2) option_2 = 0 if first_occurence != -1: option_2 = 1 + memo_solve(p1 + 1, first_occurence + 1) # Return the best option. return max(option_1, option_2) return memo_solve(0, 0)
Given two strings text1 and text2, return the length of their longest common subsequence. A subsequence of a string is a new string generated from the original string with some characters(can be none) deleted without changing the relative order of the remaining characters. (eg, "ace" is a subsequence of "abcde" while "aec" is not). A common subsequence of two strings is a subsequence that is common to both strings.   If there is no common subsequence, return 0.   Example 1: Input: text1 = "abcde", text2 = "ace" Output: 3 Explanation: The longest common subsequence is "ace" and its length is 3. Example 2: Input: text1 = "abc", text2 = "abc" Output: 3 Explanation: The longest common subsequence is "abc" and its length is 3. Example 3: Input: text1 = "abc", text2 = "def" Output: 0 Explanation: There is no such common subsequence, so the result is 0.   Constraints: 1 <= text1.length <= 1000 1 <= text2.length <= 1000 The input strings consist of lowercase English characters only.
from functools import lru_cache class Solution: def longestCommonSubsequence(self, txt1: str, txt2: str) -> int: @lru_cache(maxsize=None) def rec(i, j): if i == -1 or j == -1: return 0 if txt1[i] == txt2[j]: return rec(i-1, j-1) + 1 if txt1[i] != txt2[j]: return max(rec(i-1, j), rec(i, j-1)) return rec(len(txt1) - 1, len(txt2) - 1)
Given two strings text1 and text2, return the length of their longest common subsequence. A subsequence of a string is a new string generated from the original string with some characters(can be none) deleted without changing the relative order of the remaining characters. (eg, "ace" is a subsequence of "abcde" while "aec" is not). A common subsequence of two strings is a subsequence that is common to both strings.   If there is no common subsequence, return 0.   Example 1: Input: text1 = "abcde", text2 = "ace" Output: 3 Explanation: The longest common subsequence is "ace" and its length is 3. Example 2: Input: text1 = "abc", text2 = "abc" Output: 3 Explanation: The longest common subsequence is "abc" and its length is 3. Example 3: Input: text1 = "abc", text2 = "def" Output: 0 Explanation: There is no such common subsequence, so the result is 0.   Constraints: 1 <= text1.length <= 1000 1 <= text2.length <= 1000 The input strings consist of lowercase English characters only.
class Solution: def longestCommonSubsequence(self, text1: str, text2: str) -> int: memo = {} def dpRecurse(i : int, j : int): result = 0 if (i, j) in memo: return memo[(i, j)] if i >= len(text1) or j >= len(text2): return 0; elif text1[i] == text2[j]: result = 1 + dpRecurse(i + 1, j + 1) else: result = max(dpRecurse(i + 1, j), dpRecurse(i, j + 1)) memo[(i, j)] = result return result return dpRecurse(0, 0)
Given two strings text1 and text2, return the length of their longest common subsequence. A subsequence of a string is a new string generated from the original string with some characters(can be none) deleted without changing the relative order of the remaining characters. (eg, "ace" is a subsequence of "abcde" while "aec" is not). A common subsequence of two strings is a subsequence that is common to both strings.   If there is no common subsequence, return 0.   Example 1: Input: text1 = "abcde", text2 = "ace" Output: 3 Explanation: The longest common subsequence is "ace" and its length is 3. Example 2: Input: text1 = "abc", text2 = "abc" Output: 3 Explanation: The longest common subsequence is "abc" and its length is 3. Example 3: Input: text1 = "abc", text2 = "def" Output: 0 Explanation: There is no such common subsequence, so the result is 0.   Constraints: 1 <= text1.length <= 1000 1 <= text2.length <= 1000 The input strings consist of lowercase English characters only.
class Solution: def longestCommonSubsequence(self, text1: str, text2: str) -> int: return self.lcs(text1, text2, len(text1) - 1, len(text2) - 1, {}) def lcs(self, text1, text2, i, j, memo): if i < 0 or j < 0: return 0 if (i, j) in memo: return memo[(i, j)] if text1[i] == text2[j]: return 1 + self.lcs(text1, text2, i - 1, j - 1, memo) memo[(i, j)] = max( self.lcs(text1, text2, i - 1, j, memo), self.lcs(text1, text2, i, j - 1, memo), ) return memo[(i, j)]
Given two strings text1 and text2, return the length of their longest common subsequence. A subsequence of a string is a new string generated from the original string with some characters(can be none) deleted without changing the relative order of the remaining characters. (eg, "ace" is a subsequence of "abcde" while "aec" is not). A common subsequence of two strings is a subsequence that is common to both strings.   If there is no common subsequence, return 0.   Example 1: Input: text1 = "abcde", text2 = "ace" Output: 3 Explanation: The longest common subsequence is "ace" and its length is 3. Example 2: Input: text1 = "abc", text2 = "abc" Output: 3 Explanation: The longest common subsequence is "abc" and its length is 3. Example 3: Input: text1 = "abc", text2 = "def" Output: 0 Explanation: There is no such common subsequence, so the result is 0.   Constraints: 1 <= text1.length <= 1000 1 <= text2.length <= 1000 The input strings consist of lowercase English characters only.
class Solution: def longestCommonSubsequence(self, text1: str, text2: str) -> int: return self.find({}, 0, 0, text1, text2) def find(self, memo, i, j, text1, text2): if (i, j) in memo: return memo[(i, j)] if i == len(text1): return 0 if j == len(text2): return 0 if text1[i] == text2[j]: memo[(i, j)] = self.find(memo, i+1, j+1, text1, text2) + 1 else: memo[(i, j)] = max(self.find(memo, i, j+1, text1, text2), self.find(memo, i+1, j, text1, text2)) return memo[(i, j)]
Given two strings text1 and text2, return the length of their longest common subsequence. A subsequence of a string is a new string generated from the original string with some characters(can be none) deleted without changing the relative order of the remaining characters. (eg, "ace" is a subsequence of "abcde" while "aec" is not). A common subsequence of two strings is a subsequence that is common to both strings.   If there is no common subsequence, return 0.   Example 1: Input: text1 = "abcde", text2 = "ace" Output: 3 Explanation: The longest common subsequence is "ace" and its length is 3. Example 2: Input: text1 = "abc", text2 = "abc" Output: 3 Explanation: The longest common subsequence is "abc" and its length is 3. Example 3: Input: text1 = "abc", text2 = "def" Output: 0 Explanation: There is no such common subsequence, so the result is 0.   Constraints: 1 <= text1.length <= 1000 1 <= text2.length <= 1000 The input strings consist of lowercase English characters only.
from functools import lru_cache class Solution: def longestCommonSubsequence(self, text1: str, text2: str) -> int: @lru_cache(maxsize=None) def rec(txt1, txt2, len1, len2): if len1 == 0 or len2 == 0: return 0 if txt1[len1-1] == txt2[len2-1]: return rec(txt1, txt2, len1-1, len2-1) + 1 else: return max(rec(txt1, txt2, len1-1, len2), rec(txt1, txt2, len1, len2-1)) return rec(text1, text2, len(text1), len(text2))
Given two strings text1 and text2, return the length of their longest common subsequence. A subsequence of a string is a new string generated from the original string with some characters(can be none) deleted without changing the relative order of the remaining characters. (eg, "ace" is a subsequence of "abcde" while "aec" is not). A common subsequence of two strings is a subsequence that is common to both strings.   If there is no common subsequence, return 0.   Example 1: Input: text1 = "abcde", text2 = "ace" Output: 3 Explanation: The longest common subsequence is "ace" and its length is 3. Example 2: Input: text1 = "abc", text2 = "abc" Output: 3 Explanation: The longest common subsequence is "abc" and its length is 3. Example 3: Input: text1 = "abc", text2 = "def" Output: 0 Explanation: There is no such common subsequence, so the result is 0.   Constraints: 1 <= text1.length <= 1000 1 <= text2.length <= 1000 The input strings consist of lowercase English characters only.
import math class Solution: def longestCommonSubsequence(self, text1: str, text2: str) -> int: m = len(text1) n = len(text2) if m == 0 or n == 0: return 0 mat = [[-math.inf]*(n+1) for _ in range(m+1)] prev = [0]*(n+1) curr = [0] + [-math.inf]*n for a in range(1, m+1): for b in range(1, n + 1): if text1[a-1] == text2[b-1]: curr[b] = 1 + prev[b-1] else: curr[b] = max(curr[b-1], prev[b]) prev = curr curr = [0] + [-math.inf]*n return prev[n]
Given two strings text1 and text2, return the length of their longest common subsequence. A subsequence of a string is a new string generated from the original string with some characters(can be none) deleted without changing the relative order of the remaining characters. (eg, "ace" is a subsequence of "abcde" while "aec" is not). A common subsequence of two strings is a subsequence that is common to both strings.   If there is no common subsequence, return 0.   Example 1: Input: text1 = "abcde", text2 = "ace" Output: 3 Explanation: The longest common subsequence is "ace" and its length is 3. Example 2: Input: text1 = "abc", text2 = "abc" Output: 3 Explanation: The longest common subsequence is "abc" and its length is 3. Example 3: Input: text1 = "abc", text2 = "def" Output: 0 Explanation: There is no such common subsequence, so the result is 0.   Constraints: 1 <= text1.length <= 1000 1 <= text2.length <= 1000 The input strings consist of lowercase English characters only.
class Solution: def longestCommonSubsequence(self, text1: str, text2: str) -> int: memo = {} return self.helper(0,0,text1,text2,memo) def helper(self,index_1,index_2,text1,text2,memo): if (index_1 == len(text1) or index_2 == len(text2)): return 0 elif (index_1,index_2) in memo: return memo[(index_1,index_2)] else: if (text1[index_1] == text2[index_2]): ans = 1 + self.helper(index_1 + 1,index_2 + 1,text1,text2,memo) else: ans = max(self.helper(index_1 + 1,index_2,text1,text2,memo), self.helper(index_1,index_2 + 1,text1,text2,memo)) memo[(index_1,index_2)] = ans return ans
Given two strings text1 and text2, return the length of their longest common subsequence. A subsequence of a string is a new string generated from the original string with some characters(can be none) deleted without changing the relative order of the remaining characters. (eg, "ace" is a subsequence of "abcde" while "aec" is not). A common subsequence of two strings is a subsequence that is common to both strings.   If there is no common subsequence, return 0.   Example 1: Input: text1 = "abcde", text2 = "ace" Output: 3 Explanation: The longest common subsequence is "ace" and its length is 3. Example 2: Input: text1 = "abc", text2 = "abc" Output: 3 Explanation: The longest common subsequence is "abc" and its length is 3. Example 3: Input: text1 = "abc", text2 = "def" Output: 0 Explanation: There is no such common subsequence, so the result is 0.   Constraints: 1 <= text1.length <= 1000 1 <= text2.length <= 1000 The input strings consist of lowercase English characters only.
class Solution: def longestCommonSubsequence(self, text1: str, text2: str) -> int: hist = {} def helper(str1, str2, m, n): if (m, n) in hist: return hist[(m, n)] if m == 0 or n == 0: return 0 elif str1[m-1] == str2[n-1]: hist[(m, n)] = 1 + helper(str1, str2, m-1, n-1) return hist[(m, n)] hist[(m, n)] = max( helper(str1, str2, m-1, n), helper(str1, str2, m, n-1) ) return hist[(m, n)] return helper(text1, text2, len(text1), len(text2))
Given two strings text1 and text2, return the length of their longest common subsequence. A subsequence of a string is a new string generated from the original string with some characters(can be none) deleted without changing the relative order of the remaining characters. (eg, "ace" is a subsequence of "abcde" while "aec" is not). A common subsequence of two strings is a subsequence that is common to both strings.   If there is no common subsequence, return 0.   Example 1: Input: text1 = "abcde", text2 = "ace" Output: 3 Explanation: The longest common subsequence is "ace" and its length is 3. Example 2: Input: text1 = "abc", text2 = "abc" Output: 3 Explanation: The longest common subsequence is "abc" and its length is 3. Example 3: Input: text1 = "abc", text2 = "def" Output: 0 Explanation: There is no such common subsequence, so the result is 0.   Constraints: 1 <= text1.length <= 1000 1 <= text2.length <= 1000 The input strings consist of lowercase English characters only.
class Solution: def longestCommonSubsequence(self, text1: str, text2: str) -> int: def _longest_common_subsequence(i, j): if i < 0 or j < 0: return 0 if (i, j) not in mem: if text1[i] != text2[j]: mem[(i, j)] = max(_longest_common_subsequence(i - 1, j), _longest_common_subsequence(i, j - 1)) else: mem[(i, j)] = _longest_common_subsequence(i - 1, j - 1) + 1 return mem[(i, j)] mem = {} return _longest_common_subsequence(len(text1) - 1, len(text2) - 1)
Given two strings text1 and text2, return the length of their longest common subsequence. A subsequence of a string is a new string generated from the original string with some characters(can be none) deleted without changing the relative order of the remaining characters. (eg, "ace" is a subsequence of "abcde" while "aec" is not). A common subsequence of two strings is a subsequence that is common to both strings.   If there is no common subsequence, return 0.   Example 1: Input: text1 = "abcde", text2 = "ace" Output: 3 Explanation: The longest common subsequence is "ace" and its length is 3. Example 2: Input: text1 = "abc", text2 = "abc" Output: 3 Explanation: The longest common subsequence is "abc" and its length is 3. Example 3: Input: text1 = "abc", text2 = "def" Output: 0 Explanation: There is no such common subsequence, so the result is 0.   Constraints: 1 <= text1.length <= 1000 1 <= text2.length <= 1000 The input strings consist of lowercase English characters only.
from typing import List class Solution: def longestCommonSubsequence(self, text1: str, text2: str) -> int: # dp[i][j] 表示 text1[0:i+1] 和 text2[0:j+1] 的 Longest Common Subsequence 的长度 # 其中 0 <= i < len(text1) # 其中 0 <= j < len(text2) dp: List[List[int]] = [] for i in range(len(text1)): dp.append(len(text2) * [-1]) def cal_with_cache(i: int, j: int): if i < 0 or j < 0: return 0 if dp[i][j] == -1: dp[i][j] = cal(i, j) return dp[i][j] def cal(i: int, j: int): if i == 0 and j == 0: return 1 if text1[i] == text2[j] else 0 if text1[i] == text2[j]: return cal_with_cache(i - 1, j - 1) + 1 else: return max(cal_with_cache(i - 1, j), cal_with_cache(i, j - 1)) for i in range(len(text1)): for j in range(len(text2)): cal_with_cache(i, j) return cal_with_cache(len(text1) - 1, len(text2) - 1)
Given two strings text1 and text2, return the length of their longest common subsequence. A subsequence of a string is a new string generated from the original string with some characters(can be none) deleted without changing the relative order of the remaining characters. (eg, "ace" is a subsequence of "abcde" while "aec" is not). A common subsequence of two strings is a subsequence that is common to both strings.   If there is no common subsequence, return 0.   Example 1: Input: text1 = "abcde", text2 = "ace" Output: 3 Explanation: The longest common subsequence is "ace" and its length is 3. Example 2: Input: text1 = "abc", text2 = "abc" Output: 3 Explanation: The longest common subsequence is "abc" and its length is 3. Example 3: Input: text1 = "abc", text2 = "def" Output: 0 Explanation: There is no such common subsequence, so the result is 0.   Constraints: 1 <= text1.length <= 1000 1 <= text2.length <= 1000 The input strings consist of lowercase English characters only.
import numpy as np class Solution: def longestCommonSubsequence(self, text1: str, text2: str) -> int: l1 = len(text1) l2 = len(text2) dp = np.zeros((l1+1, l2+1), dtype = int) dp[:][0] = 0 dp[0][:] = 0 for i in range(1, l1+1): for j in range(1, l2+1): if text1[i-1] == text2[j-1]: dp[i][j] = 1 + dp[i-1][j-1] else: dp[i][j] = max(dp[i-1][j], dp[i][j-1]) return dp[-1][-1]
Given two strings text1 and text2, return the length of their longest common subsequence. A subsequence of a string is a new string generated from the original string with some characters(can be none) deleted without changing the relative order of the remaining characters. (eg, "ace" is a subsequence of "abcde" while "aec" is not). A common subsequence of two strings is a subsequence that is common to both strings.   If there is no common subsequence, return 0.   Example 1: Input: text1 = "abcde", text2 = "ace" Output: 3 Explanation: The longest common subsequence is "ace" and its length is 3. Example 2: Input: text1 = "abc", text2 = "abc" Output: 3 Explanation: The longest common subsequence is "abc" and its length is 3. Example 3: Input: text1 = "abc", text2 = "def" Output: 0 Explanation: There is no such common subsequence, so the result is 0.   Constraints: 1 <= text1.length <= 1000 1 <= text2.length <= 1000 The input strings consist of lowercase English characters only.
class Solution: def longestCommonSubsequence(self, text1: str, text2: str) -> int: memo = {} if not text1 or not text2: return 0 def lcs(i,j): if i == len(text1) or j == len(text2): return 0 if (i,j) in memo: return memo[(i,j)] if text1[i] == text2[j]: ans = 1 + lcs(i+1,j+1) else: ans = max(lcs(i+1,j), lcs(i,j+1)) memo[(i,j)] = ans return ans return lcs(0,0)
Given two strings text1 and text2, return the length of their longest common subsequence. A subsequence of a string is a new string generated from the original string with some characters(can be none) deleted without changing the relative order of the remaining characters. (eg, "ace" is a subsequence of "abcde" while "aec" is not). A common subsequence of two strings is a subsequence that is common to both strings.   If there is no common subsequence, return 0.   Example 1: Input: text1 = "abcde", text2 = "ace" Output: 3 Explanation: The longest common subsequence is "ace" and its length is 3. Example 2: Input: text1 = "abc", text2 = "abc" Output: 3 Explanation: The longest common subsequence is "abc" and its length is 3. Example 3: Input: text1 = "abc", text2 = "def" Output: 0 Explanation: There is no such common subsequence, so the result is 0.   Constraints: 1 <= text1.length <= 1000 1 <= text2.length <= 1000 The input strings consist of lowercase English characters only.
class Solution: def longestCommonSubsequence(self, text1: str, text2: str) -> int: # dp = [[0] * (len(text1) + 1) for _ in range(len(text2) + 1)] # # logic: if same, add 1, else max(try each without first) # # if row,col V == same, == prev diag + 1 # # else: = max(row-1,col, row,col-1) # for row in range(1, len(text2) + 1): # for col in range(1, len(text1) + 1): # if text2[row - 1] == text1[col - 1]: # dp[row][col] = dp[row-1][col-1] + 1 # else: # dp[row][col] = max(dp[row-1][col], dp[row][col-1]) # return dp[-1][-1] from functools import lru_cache memo = {} def lcs(i, j): if i == len(text1) or j == len(text2): return 0 if (i, j) in memo: return memo[(i, j)] elif text1[i] == text2[j]: r = 1 + lcs(i + 1, j + 1) else: r = max(lcs(i + 1, j), lcs(i, j + 1)) memo[(i, j)] = r return r return lcs(0, 0)
Given two strings text1 and text2, return the length of their longest common subsequence. A subsequence of a string is a new string generated from the original string with some characters(can be none) deleted without changing the relative order of the remaining characters. (eg, "ace" is a subsequence of "abcde" while "aec" is not). A common subsequence of two strings is a subsequence that is common to both strings.   If there is no common subsequence, return 0.   Example 1: Input: text1 = "abcde", text2 = "ace" Output: 3 Explanation: The longest common subsequence is "ace" and its length is 3. Example 2: Input: text1 = "abc", text2 = "abc" Output: 3 Explanation: The longest common subsequence is "abc" and its length is 3. Example 3: Input: text1 = "abc", text2 = "def" Output: 0 Explanation: There is no such common subsequence, so the result is 0.   Constraints: 1 <= text1.length <= 1000 1 <= text2.length <= 1000 The input strings consist of lowercase English characters only.
import numpy as np class Solution: def longestCommonSubsequence(self, text1: str, text2: str) -> int: l1 = len(text1) l2 = len(text2) dp = np.zeros((l1+1, l2+1), dtype = int) for i in range(l1+1): dp[i][0] = 0 for i in range(l2+1): dp[0][i] = 0 for i in range(1, l1+1): for j in range(1, l2+1): if text1[i-1] == text2[j-1]: dp[i][j] = 1 + dp[i-1][j-1] else: dp[i][j] = max(dp[i-1][j], dp[i][j-1]) return dp[-1][-1]