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.
|
from functools import lru_cache
class Solution:
def longestCommonSubsequence(self, text1: str, text2: str) -> int:
'''
base case:
text1 = c1, text2 = c2
if c1 == c2 return 1, if c1 != c2 return 0
text1 = sub1 + c1 -> c1 is the last char
text2 = sub2 + c2
DP(text1, text2) = max(DP(text1, sub2), DP(sub1, text2), DP(sub1, sub2))
or DP(sub1, sub2) + 1 if c1 == c2
'''
if len(text1) == 0 or len(text2) == 0:
return 0
@lru_cache(None)
def lcs_of_sub(s1, s2):
if not s1 or not s2:
return 0
c1, c2 = s1[-1], s2[-1]
sub1, sub2 = s1[:-1], s2[:-1]
# print(sub1, c1, sub2, c2)
if c1 == c2:
return lcs_of_sub(sub1, sub2) + 1
return max(
lcs_of_sub(sub1, sub2), lcs_of_sub(sub1, s2), lcs_of_sub(s1, sub2)
)
return lcs_of_sub(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:
memo={}
def LCS(s1,s2, i1, i2):
if i1>len(s1)-1 or i2>len(s2)-1: return 0
if (i1,i2) in memo: return memo[(i1,i2)]
if s1[i1]==s2[i2]:
memo[(i1,i2)]=1+ LCS(s1,s2,i1+1,i2+1)
return memo[(i1,i2)]
s1_next=LCS(s1,s2,i1+1,i2)
s2_next=LCS(s1,s2,i1,i2+1)
memo[(i1,i2)]= max(s1_next,s2_next)
return memo[(i1,i2)]
return LCS(text1,text2,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 = [[None for i in range(len(text2)+1)] for _ in range(len(text1)+1)]
def rec(i, j):
if i == len(text1) or j == len(text2):
return 0
if text1[i] == text2[j]:
if not memo[i+1][j+1]:
memo[i+1][j+1] = rec(i+1, j+1)
return 1 + memo[i+1][j+1]
else:
if not memo[i][j+1]:
memo[i][j+1] = rec(i, j+1)
if not memo[i+1][j]:
memo[i+1][j] = rec(i+1, j)
return max(memo[i][j+1], memo[i+1][j])
return rec(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 __init__(self):
self.cached = {}
def longestCommonSubsequence(self, text1: str, text2: str) -> int:
if len(text1) == 0 or len(text2) == 0:
return 0
if (len(text1), len(text2)) in self.cached:
return self.cached[(len(text1), len(text2))]
if text1[-1] == text2[-1]:
r = self.longestCommonSubsequence(text1[:-1], text2[:-1]) + 1
else:
r = max(
self.longestCommonSubsequence(text1, text2[:-1]),
self.longestCommonSubsequence(text1[:-1], text2)
)
self.cached[(len(text1), len(text2))] = r
return r
|
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:
@lru_cache(maxsize = None)
def longest(self, p1, p2):
if p1 == len(self.t1) or p2 == len(self.t2):
return 0
if self.t1[p1] == self.t2[p2]:
return 1 + self.longest(p1 + 1, p2 + 1)
return max(self.longest(p1 + 1, p2), self.longest(p1, p2 + 1))
def longestCommonSubsequence(self, text1: str, text2: str) -> int:
self.t1 = text1
self.t2 = text2
return self.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.
|
class Solution:
def longestCommonSubsequence(self, str1: str, str2: str) -> int:
cache = [ [-1]*len(str2) for _ in str1]
def checkPosition(i1, i2):
if i1 == len(str1) or i2 == len(str2):
return 0
#hashing
if cache[i1][i2] != -1:
return cache[i1][i2]
# the characters match, so we can keep both
if str1[i1] == str2[i2]:
keep = 1 + checkPosition(i1+1, i2+1)
else:
keep = -1
skip1 = checkPosition(i1+1, i2)
skip2 = checkPosition(i1, i2+1)
best = max([keep, skip1, skip2])
cache[i1][i2] = best
return best
return checkPosition(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 _longest_common_subsequence(self, s1, s2, cache):
if (s1, s2) in cache:
return cache[(s1, s2)]
elif len(s1) == 0 or len(s2) == 0:
cache[(s1, s2)] = 0
return 0
elif s1[-1] == s2[-1]:
answer = 1 + self._longest_common_subsequence(s1[:-1], s2[:-1], cache)
cache[(s1, s2)] = answer
return answer
else:
answer = max(self._longest_common_subsequence(s1, s2[:-1], cache), self._longest_common_subsequence(s1[:-1], s2, cache))
cache[(s1, s2)] = answer
return answer
def longestCommonSubsequence(self, s1: str, s2: str) -> int:
return self._longest_common_subsequence(s1, 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:
def lcs(i,j):
if i==len(text1) or j==len(text2):
return 0
if tuple([i,j]) in dp:
return dp[tuple([i,j])]
if text1[i]==text2[j]:
dp[tuple([i,j])]=1+lcs(i+1,j+1)
return dp[tuple([i,j])]
dp[tuple([i,j])]=max(lcs(i+1,j),lcs(i,j+1))
return dp[tuple([i,j])]
dp={}
# text1+=\"0\"
# text2+=\"0\"
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:
def rec(s1, s2, i, j, lcs):
if i == len(s1) or j == len(s2):
return 0
if (i, j) in lcs:
return lcs[(i, j)]
else:
if s1[i] == s2[j]:
lcs[(i, j)] = 1 + rec(s1, s2, i + 1, j + 1, lcs)
else:
l = rec(s1, s2, i + 1, j, lcs)
r = rec(s1, s2, i, j + 1, lcs)
lcs[(i, j)] = max(l, r)
return lcs[(i, j)]
return rec(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.
|
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 _longest_common_subsequence(self, s1, s2, cache):
if (s1, s2) in cache:
return cache[(s1, s2)]
elif len(s1) == 0 or len(s2) == 0:
return 0
elif s1[-1] == s2[-1]:
answer = 1 + self._longest_common_subsequence(s1[:-1], s2[:-1], cache)
cache[(s1, s2)] = answer
return answer
else:
answer = max(self._longest_common_subsequence(s1, s2[:-1], cache), self._longest_common_subsequence(s1[:-1], s2, cache))
cache[(s1, s2)] = answer
return answer
def longestCommonSubsequence(self, s1, s2):
return self._longest_common_subsequence(s1, 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:
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]
includeHere = 0
if (text1[i] == text2[j]):
includeHere = 1 + memoSolve(i + 1, j + 1)
includeLater = memoSolve(i, j+1)
included = max(includeHere, includeLater)
noTake = max(memoSolve(i + 1, j), memoSolve(i+1, j+1))
memo[i][j] = max(included, noTake)
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:
@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:
# dp[i][j] = dp[i-1][j-1] + 1 if text1[i] == text2[j]
# else max(dp[i-1][j], dp[i][j-1])
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] = dp[i-1][j-1] + 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 = [[0]*len(text2) for i in range(len(text1))]
def helper(s1,s2,i1,i2):
if not s1 or not s2:
return 0
if memo[i1][i2]:
return memo[i1][i2]
if s1[0] == s2[0]:
memo[i1][i2] = 1+helper(s1[1:],s2[1:],i1+1,i2+1)
else:
memo[i1][i2] = max(helper(s1[1:],s2,i1+1,i2),helper(s1,s2[1:],i1,i2+1))
return memo[i1][i2]
return helper(text1,text2,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, m = len(text1), len(text2)
dp = [[0] * (m + 1) for _ in range(n + 1)]
for row in range(1, n + 1):
for col in range(1, m + 1):
if text1[row - 1] == text2[col - 1]:
dp[row][col] = 1 + dp[row - 1][col - 1]
else:
dp[row][col] = max(dp[row - 1][col], dp[row][col - 1])
return dp[n][m]
def longestCommonSubsequence1(self, text1: str, text2: str) -> int:
def helper(i1, i2):
if (i1, i2) in self.memo:
return self.memo[(i1, i2)]
res = 0
if i1 == len(text1) or i2 == len(text2):
res = 0
elif text1[i1] == text2[i2]:
res = 1 + helper(i1 + 1, i2 + 1)
else:
res = max(helper(i1 + 1, i2), helper(i1, i2 + 1))
self.memo[(i1, i2)] = res
return self.memo[(i1, i2)]
self.memo = {}
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:
n,m=len(text1),len(text2)
dp=[[0]*(m+1) for _ in range(n+1)]
for i in range(n):
for j in range(m):
if text1[i]==text2[j]:
dp[i+1][j+1]=dp[i][j]+1
else:
dp[i+1][j+1]=max(dp[i][j+1],dp[i+1][j])
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:
dp=[[0]*(len(text2)+1) for _ in range(len(text1)+1)]
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[-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 helper(self, text1, text2, index1, index2, match):
if index1 == len(text1) or index2 == len(text2):
return match
s1 = 0
s2 = 0
s3 = 0
# case - match
if text1[index1] == text2[index2]:
s1 = self.helper(text1, text2, index1+1, index2+1, match+1)
else:
# case - did not match
s2 = self.helper(text1, text2, index1, index2+1, match)
s3 = self.helper(text1, text2, index1+1, index2, match)
return max(s1, s2, s3)
def longestCommonSubsequence(self, text1: str, text2: str) -> int:
if not text1 or not text2:
return 0
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])):
if text1[j-1] == text2[i-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[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, stringA: str, stringB: str) -> int:
lengthA = len(stringA)
lengthB = len(stringB)
dp = [[ 0 for j in range(lengthB + 1)] for i in range(lengthA + 1)]
for i in range(1, lengthA + 1):
for j in range(1, lengthB + 1):
if stringA[i - 1] == stringB[j - 1]:
dp[i][j] = dp[i - 1][j - 1] + 1
else:
dp[i][j] = max(dp[i][j - 1], dp[i - 1][j])
return dp[lengthA][lengthB]
|
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:
len1 = len(text1)
len2 = len(text2)
dp = [[0 for _ in range(len2+1)] for _ in range(len1+1)]
for i in range(len1):
for j in range(len2):
if text1[i] == text2[j]:
dp[i+1][j+1] = dp[i][j]+1
else:
dp[i+1][j+1] = max(dp[i][j+1],dp[i+1][j])
return dp[len1][len2]
|
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:
# use dynamic programming
# initialize table to store sub solutions
# row (i) = index of text1 + 0 for initial row
# column (j) = index of text2 + 0 for initial row
# cell = max length of subsequence for text1[1:i] text2[1:j]
t = [[0 for _ in range(len(text2)+1)] for _ in range(len(text1)+1)]
# loop through each element
for i in range(1, len(text1)+1):
for j in range(1, len(text2)+1):
# if text is the same, diagonal cell + 1
if text1[i-1] == text2[j-1]:
t[i][j] = t[i-1][j-1] + 1
# else, max of above (not considering last of text 1) or left
else:
t[i][j] = max(t[i-1][j], t[i][j-1])
# return right bottom of table
return t[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 not text1 or not text2:
return 0
m, n = len(text1), len(text2)
dp = [[0 for _ in range(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] = 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.
|
class Solution:
def longestCommonSubsequence(self, text1: str, text2: str) -> int:
# dp
if not text1 or not text2:
return 0
m, n = len(text1), len(text2)
dp = [[0 for _ in range(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] = 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.
|
class Solution:
def longestCommonSubsequence(self, text1: str, text2: str) -> int:
row = len(text1) + 1
col = len(text2) + 1
dp = [[0 for _ in range(col)] for _ in range(row)]
for i in range(1,row):
for j in range(1,col):
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[-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:
ans = 0
dp = [[0 for _ in range(len(text1)+1)] for _ in range(len(text2)+1)]
for i in range(1, len(text2)+1):
for j in range(1, len(text1)+1):
if text2[i-1] == text1[j-1]:
dp[i][j] = dp[i-1][j-1]+1
else:
dp[i][j] = max(dp[i-1][j], dp[i][j-1])
ans = max(ans, dp[i][j])
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:
dp = [[0 for _ in range(len(text2) + 1)] for _ in range(len(text1) + 1)]
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] = dp[i - 1][j - 1] + 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:
L1=len(text1)
L2=len(text2)
A=[ [0]*(L2+1) for i in range(L1+1)]
##print(A)
Max=0
##print(L1,L2)
for i in range(L1,-1,-1):
for j in range(L2,-1,-1):
if i<L1 and j<L2:
##print(i,j)
if text1[i]==text2[j]:
A[i][j]= A[i+1][j+1]+1
else: A[i][j]=max(A[i][j+1],A[i+1][j])
if Max<A[i][j]: Max=A[i][j]
##print(A[2][3])
return A[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:
s1=text1
s2=text2
if len(s1)==0 or len(s2)==0:
return 0
rows,cols=len(s1)+1,len(s2)+1
arr = [[0 for i in range(cols)] for j in range(rows)]
for i in range(len(s1)):
for j in range(len(s2)):
if s1[i]==s2[j]:
arr[i][j]=arr[i-1][j-1]+1
if s1[i]!=s2[j]:
arr[i][j]=max(arr[i-1][j],arr[i][j-1])
return (arr[rows-2][cols-2])
|
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:
m,n = len(A), len(B)
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 or j==0:
dp[i][j] =0
elif A[i-1] == B[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[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:
dp = [0]*(len(text2)+1)
for i in range(1, len(text1)+1):
prev = dp[0]
for j in range(1, len(text2)+1):
last = dp[j]
if text1[i-1]==text2[j-1]:
dp[j] = prev+1
if last>prev:
prev = last
return max(dp)
|
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:
#注意这里不要写成【0 * (len....)】
dp_grid = [[0] * (len(s1) + 1) for _ in range(len(s2) + 1)]
for r in range(len(s2) + 1):
for c in range(len(s1) + 1):
if r == 0 or c == 0:
dp_grid[r][c] = 0
#注意这里的s2和s1是string,别搞错写成s2[r-1][c-1]了
elif s2[r-1] == s1[c-1]:
dp_grid[r][c] = dp_grid[r-1][c-1] + 1
else:
dp_grid[r][c] = max(dp_grid[r-1][c], dp_grid[r][c-1])
return dp_grid[-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
dp = [[0 for i in range(len(text1))] for j in range(len(text2))]
for i in range(len(text2)):
for j in range(len(text1)):
if i == 0 and j == 0:
dp[i][j] = 1 if text1[j] == text2[i] else 0
elif i == 0:
if dp[i][j-1] == 0 and text1[j] == text2[i]: dp[i][j] = 1
else: dp[i][j] = dp[i][j-1]
elif j == 0:
dp[i][j] = 1 if dp[i-1][j] == 1 or text1[j] == text2[i] else 0
elif text1[j] == text2[i]:
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:
def lcsTab(s1,s2,m,n):
dp = [[0 for i in range(n+1)] for j in range(m+1)]
for i in range(m+1):
for j in range(n+1):
if(i==0 or j==0):
dp[i][j] = 0
elif(s1[i-1] == s2[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[m][n]
m = len(text1)
n = len(text2)
return lcsTab(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:
n=len(text1)
m=len(text2)
dp=[[0 for i in range(m+1)] for i in range(n+1)]
for i in range(1,n+1):
for j in range(1,m+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])
print(dp)
return 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:
dynamic_array = [[0 for c in range(len(text2)+1)] for ch in range(len(text1)+1)]
for i in range(len(text1)+1):
for j in range(len(text2)+1):
if i == 0 or j == 0:
dynamic_array[i][j] = 0
elif text1[i-1] == text2[j-1]:
dynamic_array[i][j] = 1 + dynamic_array[i-1][j-1]
else:
dynamic_array[i][j] = max(dynamic_array[i-1][j],dynamic_array[i][j-1])
return dynamic_array[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:
l1 = len(text1)
l2 = len(text2)
dp = [[0 for c in range(l2+1)] for r in range(l1+1)]
for i in range(l1):
for j in range(l2):
dp[i+1][j+1] = max(dp[i+1][j], dp[i][j+1], dp[i][j] + (text1[i]==text2[j]))
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:
dp = [[0 for i in range(len(text1)+1)] for j in range(len(text2)+1)]
for i in range(1, len(text2)+1):
for j in range(1, len(text1)+1):
if text2[i-1] == text1[j-1]:
dp[i][j] = 1 + dp[i-1][j-1]
else:
dp[i][j] = max(dp[i][j-1], dp[i-1][j])
print(dp)
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:
dp=[[-1 for i in range(len(text2)+1)]for j in range(len(text1)+1)]
for i in range(len(text1)+1):
for j in range(len(text2)+1):
if i==0 or j==0:
dp[i][j]=0
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]=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)]
|
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)
if l1 == 0 or l2 == 0:
return 0
dp = [0 for _ in range(l1+1)]
for i in range(l2):
temp = [n for n in dp]
curMax = 0
for j in range(1, l1+1):
if temp[j-1] > curMax:
curMax = temp[j-1]
if text2[i] == text1[j-1]:
dp[j] = curMax + 1
return max(dp)
|
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 = len(text1)
L2 = len(text2)
dp = [[0 for _ in range(L2 + 1)] for _ in range(L1 + 1)]
for i in range(1, L1 + 1):
for j in range(1, L2 + 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 - 1], 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:
dp = [[0 for j in range(len(text2))] for i in range(len(text1))]
for i in range(len(text1)):
for j in range(len(text2)):
if text1[i] == text2[j]:
if not i or not j:
dp[i][j] = 1
else:
dp[i][j] = dp[i-1][j-1] + 1
else:
if i:
dp[i][j] = dp[i-1][j]
if j:
dp[i][j] = max(dp[i][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:
s1=text1
s2=text2
if len(s1)==0 or len(s2)==0:
return 0
rows=len(s1)+1
cols=len(s2)+1
arr=[[0 for i in range(cols)] for j in range(rows)]
for i in range(len(s1)):
for j in range(len(s2)):
if s1[i]==s2[j]:
arr[i+1][j+1]=arr[i][j]+1
if s1[i]!=s2[j]:
arr[i+1][j+1]=max(arr[i][j+1],arr[i+1][j])
return(arr[-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,m=len(text1),len(text2)
dp = [[0 for i in range(m+1)] for j in range(n+1)]
for i in range(1,n+1):
for j in range(1,m+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[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, a: str, b: str) -> int:
m, n = len(a), len(b)
dp = [[0 for _ in range(n)] for _ in range(m)]
for i in range(m):
for j in range(n):
if a[i] == b[j]:
dp[i][j] = 1 + (dp[i - 1][j - 1] if i - 1 >= 0 and j - 1 >= 0 else 0)
else:
dp[i][j] = max(dp[i][j - 1] if j - 1 >= 0 else 0, dp[i - 1][j] if i - 1 >= 0 else 0)
print(dp)
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:
## recurrsion => lead to time exceeded
# if not text1 or not text2:
# return 0
# if text1[-1]==text2[-1]:
# return 1+self.longestCommonSubsequence(text1[0:-1],text2[0:-1])
# elif text1[-1] != text2[-1]:
# t1 = self.longestCommonSubsequence(text1,text2[0:-1])
# t2 = self.longestCommonSubsequence(text1[0:-1],text2)
# return max(t1,t2)
## memorized method
# n = len(text1)
# m = len(text2)
# arr = [[0] * (m+1) for _ in range(n+1)]
# return self.LCS(text1,text2,n,m,arr)
# def LCS(self,text1,text2,n,m,arr):
# if arr[n][m]:
# return arr[n][m]
# if n==0 or m==0:
# res = 0
# elif text1[n-1]==text2[m-1]:
# res = 1+self.LCS(text1,text2,n-1,m-1,arr)
# else:
# t1 = self.LCS(text1,text2,n,m-1,arr)
# t2 = self.LCS(text1,text2,n-1,m,arr)
# res = max(t1,t2)
# arr[n][m] = res
# return res
# bottom-up method
n,m = len(text1), len(text2)
arr = [[0]*(m+1) for _ in range(n+1)]
for i in range(n+1):
for j in range(m+1):
if i==0 or j==0:
arr[i][j] = 0
elif text1[i-1]==text2[j-1]:
arr[i][j] = 1 + arr[i-1][j-1]
else:
arr[i][j] = max(arr[i-1][j],arr[i][j-1])
return arr[-1][-1] # arr[-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:
d = [[None] * (len(text2)+1) for _ in range(len(text1)+1)]
for j in range(len(text2)+1):
d[0][j] = 0
for i in range(len(text1)+1):
d[i][0] = 0
i = 1
while i < len(text1) + 1:
j = 1
while j < len(text2) + 1:
if text1[i-1] == text2[j-1]:
d[i][j] = max(d[i-1][j-1] + 1, d[i][j-1], d[i-1][j])
else:
d[i][j] = max(d[i][j-1], d[i-1][j])
j += 1
i += 1
return max([max(row) for row in d])
|
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:
table={}
def helper(t1, t2, i,j, table):
if table.get((i,j)) != None:
return table[(i,j)]
n = len(t1)
m = len(t2)
if m <=j or n <= i:
return 0;
v = t1[i]
idx = t2.find(v, j)
if idx != -1:
r = max(helper(t1, t2, i+1, j, table), helper(t1, t2, i+1, idx+1, table)+1)
table[(i,j)] = r
else:
r = helper(t1, t2, i+1,j, table)
table[(i,j)] = r
return table[(i,j)]
return helper(text1, text2, 0, 0, table)
def longestCommonSubsequence(self, text1: str, text2: str) -> int:
n = len(text1)
m = len(text2)
dp=[]
for v in range(n+1):
dp.append([-1]*(m+1))
dp[0][0] = 0
for i in range(n+1):
dp[i][0] = 0
for j in range(m+1):
dp[0][j] = 0
for i in range(1,n+1):
for j in range(1, m+1):
v1=text1[i-1]
v2 = text2[j-1]
if v1 == v2:
dp[i][j] = max(dp[i-1][j-1]+1, dp[i-1][j], dp[i][j-1])
else:
dp[i][j] = max(dp[i-1][j-1], dp[i-1][j], dp[i][j-1])
return 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:
if not text1 or not text2:
return 0
dp = [[0 for col in range(len(text2)+1)] for row in range(len(text1)+1)]
for i in range(1,len(text1)+1):
for j in range(1,len(text2)+1):
options = [dp[i][j-1],dp[i-1][j]]
if text1[i-1] == text2[j-1]:
options.append(dp[i-1][j-1]+1)
dp[i][j] = max(options)
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:
################
dp = [0]*(len(text2)+1)
for i in range(1, len(text1)+1):
prev = dp[0]
for j in range(1, len(text2)+1):
last = dp[j]
if text1[i-1]==text2[j-1]:
dp[j] = prev+1
if last>prev:
prev = last
return max(dp)
|
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)
memo = [[0 for _ in range(m)] for _ in range(n)]
res = 0
for row in range(n):
if row == 0 and text2[row] == text1[0]:
memo[row][0] = 1
else:
memo[row][0] = memo[row-1][0]
if text2[row] == text1[0]:
memo[row][0] = 1
for col in range(m):
if col == 0 and text1[col] == text2[0]:
memo[0][col] = 1
else:
memo[0][col] = memo[0][col-1]
if text1[col] == text2[0]:
memo[0][col] = 1
print(memo)
for row in range(1, n):
for col in range(1, m):
if text1[col] != text2[row]:
memo[row][col] = max(memo[row-1][col],
memo[row][col-1],
memo[row-1][col-1])
else:
memo[row][col] = 1 + min(memo[row-1][col],
memo[row][col-1],
memo[row-1][col-1])
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:
width,depth = len(text1),len(text2)
dp = [[0 for _ in range(width)] for _ in range(depth)]
for i in range(depth):
for j in range(width):
if i == 0 and j == 0:
dp[i][j] = 0
if i == 0:
up = 0
else:
up = dp[i-1][j]
if j == 0:
left = 0
else:
left = dp[i][j - 1]
if (text2[i] == text1[j]):
if i == 0 or j == 0:
dp[i][j] = 1
else:
dp[i][j] = dp[i-1][j-1] + 1
else:
dp[i][j] = max(left,up)
for d in dp:print(d)
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:
# dp[i][j]
# represents what is the maximum up to that i and j included
dp = [[0] * (len(text2) + 1) for _ in range(len(text1) + 1)]
max_ = 0
for i in range(len(text1)-1, -1, -1):
for j in range(len(text2)-1, -1, -1):
dp[i][j] = max(dp[i+1][j], dp[i][j+1])
if text1[i] == text2[j]:
dp[i][j] = max(dp[i+1][j+1] + 1, dp[i][j])
max_ = max(max_, dp[i][j])
return max_
|
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 _ in range((len(text1)+1))]
count=0
for r in range(1, len(dp)):
for c in range(1, len(dp[0])):
if text1[r-1]==text2[c-1]:
dp[r][c]= dp[r-1][c-1]+1
else:
dp[r][c]= max(dp[r-1][c], dp[r][c-1])
count= max(count,dp[r][c])
return count
|
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 = [[0 for _ in range(m)] for _ in range(n)]
#i -> text2
#j -> text1
for i in range(n):
for j in range(m):
# Need to do j-1, i-1, otherwise we would start from 1
# on both strings
if text1[i] == text2[j]:
dp[i][j] = 1 + (0 if i-1 < 0 or j-1 < 0 else dp[i-1][j-1])
else:
if i-1 > -1:
dp[i][j] = dp[i-1][j]
if j-1 > -1:
dp[i][j] = max(dp[i][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, a: str, b: str) -> int:
m, n = len(a), len(b)
c = [[0 for _ in range(n + 1)] for _ in range(m + 1)]
for i in range(m + 1):
for j in range(n + 1):
if i == 0 or j == 0: c[i][j] = 0
else:
if a[i - 1] == b[j - 1]: c[i][j] = max([c[i - 1][j], c[i][j - 1], c[i - 1][j - 1] + 1])
else: c[i][j] = max([c[i - 1][j], c[i][j - 1]])
return c[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:
s1, s2 = len(text1), len(text2)
# init
res = 0
dp = [[0 for _ in range(s2+1)] for _ in range(s1+1)]
# dp[i][j] = dp[i-1][j-1] + 1 if text1[i]==text2[j]
# dp[i-1][j-1] else
for i in range(1, s1+1):
for j in range(1, s2+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])
res = max(dp[i][j], res)
return res
# boundary: 0 <= max_common <= min(len(tex1), 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:
m = len(text1)
n = len(text2)
dp = [[0 for _ in range(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] = dp[i - 1][j - 1] + 1
else:
dp[i][j] = max(dp[i][j - 1], dp[i - 1][j])
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.
|
class Solution:
def longestCommonSubsequence(self, A: str, B: str) -> int:
if not A or not B:
return 0
m, n = len(A), len(B)
f = [[None] * (n + 1) for _ in range(m + 1)]
for i in range(m + 1):
for j in range(n + 1):
f[i][j] = 0
if i == 0 or j == 0:
continue
f[i][j] = max(f[i][j], f[i - 1][j], f[i][j - 1])
if A[i - 1] == B[j - 1]:
f[i][j] = max(f[i][j], f[i - 1][j - 1] + 1)
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:
lengthone= len(text1)
lengthtwo= len(text2)
n = 1000
ans = [[0]*n for _ in range(n)]
for i in range(lengthone):
for j in range(lengthtwo):
if text1[i]==text2[j]:
ans[i][j]=ans[i-1][j-1]+1
else:
ans[i][j]=max(ans[i-1][j],ans[i][j-1])
return ans[lengthone-1][lengthtwo-1]
# if ans[len(text1)][len(text2)] != 0:
# return ans[len(text1)][len(text2)]
# elif len(text1) == 0 or len(text2) == 0:
# return 0
# elif text1[-1] == text2[-1]:
# ans[len(text1)][len(text2)] = LCS(text1[:-1], text2[:-1]) + 1
# return ans[len(text1)][len(text2)]
# else:
# ans[len(text1)][len(text2)] = max(
# LCS(text1[:-1], text2), LCS(text1, text2[:-1]))
# return ans[len(text1)][len(text2)]
# text1 = \"This code is a sample text for longest common subsequence\"
# text2 = \"abcde\"
# LCS(text1, text2)
# print(\"LCS of {} and {} = {}\".format(text1, text2, LCS(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:
if not text1: return 0
l = len(text1)
n = next((i for i, c in enumerate(text1) if c == text2[0]), l)
commonSubstrings = [0] * n + [1] * (l - n)
for c2 in text2[1:]:
tmp = [0] * l
tmp[0] = int(c2 == text1[0] or commonSubstrings[0])
for i, c1 in enumerate(text1[1:], 1):
if c1 == c2:
tmp[i] = commonSubstrings[i - 1] + 1
else:
tmp[i] = max(tmp[i - 1], commonSubstrings[i])
commonSubstrings = tmp
return commonSubstrings[-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:
#Top Down recursion - only works with LRU Cache
@lru_cache(None)
def helper(idx1, idx2):
if len(text1) <= idx1 or len(text2) <= idx2:
return 0
if text1[idx1] == text2[idx2]:
return 1 + helper(idx1+1, idx2+1)
if text1[idx1] not in text2[idx2:]:
return helper(idx1+1, idx2)
else:
newidx2 = idx2 + text2[idx2:].index(text1[idx1])
return max(helper(idx1+1,newidx2+1) + 1, helper(idx1+1, idx2))
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:
dp = [ [0 for _ in range(len(text1) + 1)] for _ in range(len(text2) + 1) ];
to_return = 0;
for i in range(len(text2)):
for j in range(len(text1)):
r, c = i + 1, j + 1
if(text1[j] == text2[i]):
dp[r][c] = 1 + dp[r-1][c-1]
else:
dp[r][c] = max(dp[r-1][c], dp[r][c-1])
to_return = max(to_return, dp[r][c]);
return to_return;
|
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:
# '' a. c. e
# '' 0 0. 0. 0.
# a 0 1 0. 0
# b 0 1
# c 0 1
# d 0
# e 0
if len(text1) == 0 or len(text2) == 0: return ''
DP = [[0]*(len(text1)+1) for _ in range(len(text2)+1)]
longest = 0
for i in range(1,len(text2)+1):
for j in range(1,len(text1)+1):
if text2[i-1] == text1[j-1]:
DP[i][j] = max(max(DP[i-1][j-1]+1, DP[i-1][j]),DP[i][j-1])
else:
DP[i][j] = max(DP[i-1][j],DP[i][j-1])
longest = max(longest, DP[i][j])
# print(DP)
return longest
|
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 helper(idx1, idx2):
if len(text1) <= idx1 or len(text2) <= idx2:
return 0
if text1[idx1] == text2[idx2]:
return 1 + helper(idx1+1, idx2+1)
if text1[idx1] not in text2[idx2:]:
return helper(idx1+1, idx2)
else:
newidx2 = idx2 + text2[idx2:].index(text1[idx1])
return max(helper(idx1+1,newidx2+1) + 1, helper(idx1+1, idx2))
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:
D = []
for i in range(len(text1) + 1):
D.append([0] * (len(text2) + 1))
for i in range(1, len(text1) + 1):
for j in range(1, len(text2) + 1):
delta = 1 if text1[i-1] == text2[j-1] else 0
D[i][j] = max(D[i-1][j], D[i][j-1], D[i-1][j-1] + delta)
return D[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(text1):
text1,text2=text2,text1
pre = [0] * (len(text1)+1)
cur = [0] * (len(text2)+1)
for col in reversed(list(range(len(text1)))):
for row in reversed(list(range(len(text2)))):
if (text1[col] == text2[row]):
cur[row] = 1 + pre[row+1]
else:
cur[row] = max(pre[row],cur[row + 1])
pre, cur = cur, pre
return pre[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(text2) > len(text1):
text1, text2 = text2, text1
dp = [[0] * (len(text1)+1) for _ in range(len(text2)+1)]
for i in range(1, len(text2)+1):
for j in range(1, len(text1)+1):
max_sides = max(dp[i-1][j], dp[i][j-1])
if text1[j-1] == text2[i-1]:
dp[i][j] = max(dp[i-1][j-1] + 1, max_sides)
else:
dp[i][j] = max(dp[i-1][j-1], max_sides)
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:
text1 = list(text1)
text2 = list(text2)
n = len(text1)
m = len(text2)
dp = [[0 for i in range(m+1)] for i in range(n+1)]
for i in range(1,n+1):
for j in range(1,m+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[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, s1: str, s2: str) -> int:
if s1 == '' or s2 == '':
return 0
dp = [[0 for y in s2] for x in s1]
for i in range(0, len(s1)):
for j in range(0, len(s2)):
if i == 0 and j == 0:
dp[i][j] = 1 if s1[i] == s2[j] else 0
elif i == 0:
dp[i][j] = 1 if s1[i] == s2[j] else dp[i][j - 1]
elif j == 0:
dp[i][j] = 1 if s1[i] == s2[j] else dp[i - 1][j]
else:
dp[i][j] = max([dp[i - 1][j - 1] + 1 if s1[i] == s2[j] else 0, 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
class Solution:
def longestCommonSubsequence(self, text1: str, text2: str) :
dp = np.full((len(text1)+1,len(text2)+1),0)
print((dp.shape))
for i in range(dp.shape[0]-2,-1,-1) :
for j in range(dp.shape[1]-2,-1,-1) :
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[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:
len_1, len_2 = len(text1), len(text2)
# Append an additional 0 to stand for the case of \"\" for both strings
dp = [[0] * (len_2 + 1) for _ in range(len_1 + 1)]
max_len_subseq = 0
for j in range(1, len_2 + 1):
for i in range(1, len_1 + 1):
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])
max_len_subseq = max(max_len_subseq, dp[i][j])
return max_len_subseq
|
Given a string s and a string t, check if s is subsequence of t.
You may assume that there is only lower case English letters in both s and t. t is potentially a very long (length ~= 500,000) string, and s is a short string (
A subsequence of a string is a new string which is formed from the original string by deleting some (can be none) of the characters without disturbing the relative positions of the remaining characters. (ie, "ace" is a subsequence of "abcde" while "aec" is not).
Example 1:
s = "abc", t = "ahbgdc"
Return true.
Example 2:
s = "axc", t = "ahbgdc"
Return false.
Follow up:
If there are lots of incoming S, say S1, S2, ... , Sk where k >= 1B, and you want to check one by one to see if T has its subsequence. In this scenario, how would you change your code?
Credits:Special thanks to @pbrother for adding this problem and creating all test cases.
|
class Solution:
def isSubsequence(self, s, t):
"""
:type s: str
:type t: str
:rtype: bool
"""
if len(s) > len(t):
return False
for i in s:
if i in t:
index = t.find(i)
t = t[index + 1:]
else:
return False
return True
|
Given a string s and a string t, check if s is subsequence of t.
You may assume that there is only lower case English letters in both s and t. t is potentially a very long (length ~= 500,000) string, and s is a short string (
A subsequence of a string is a new string which is formed from the original string by deleting some (can be none) of the characters without disturbing the relative positions of the remaining characters. (ie, "ace" is a subsequence of "abcde" while "aec" is not).
Example 1:
s = "abc", t = "ahbgdc"
Return true.
Example 2:
s = "axc", t = "ahbgdc"
Return false.
Follow up:
If there are lots of incoming S, say S1, S2, ... , Sk where k >= 1B, and you want to check one by one to see if T has its subsequence. In this scenario, how would you change your code?
Credits:Special thanks to @pbrother for adding this problem and creating all test cases.
|
class Solution:
def isSubsequence(self, s, t):
"""
:type s: str
:type t: str
:rtype: bool
"""
i = 0
if not s:
return True
m, n = len(t), len(s)
for ch in t:
if ch == s[i]:
if i == n-1:
return True
i += 1
return False
|
Given a string s and a string t, check if s is subsequence of t.
You may assume that there is only lower case English letters in both s and t. t is potentially a very long (length ~= 500,000) string, and s is a short string (
A subsequence of a string is a new string which is formed from the original string by deleting some (can be none) of the characters without disturbing the relative positions of the remaining characters. (ie, "ace" is a subsequence of "abcde" while "aec" is not).
Example 1:
s = "abc", t = "ahbgdc"
Return true.
Example 2:
s = "axc", t = "ahbgdc"
Return false.
Follow up:
If there are lots of incoming S, say S1, S2, ... , Sk where k >= 1B, and you want to check one by one to see if T has its subsequence. In this scenario, how would you change your code?
Credits:Special thanks to @pbrother for adding this problem and creating all test cases.
|
class Solution:
def isSubsequence(self, s, t):
"""
:type s: str
:type t: str
:rtype: bool
"""
length = len(s)
idx = 0
for i in range(length):
idx = t.find(s[i], idx)
#print(s[i])
if idx == -1:
return False
idx += 1
#print(idx)
return True
|
Given a string s and a string t, check if s is subsequence of t.
You may assume that there is only lower case English letters in both s and t. t is potentially a very long (length ~= 500,000) string, and s is a short string (
A subsequence of a string is a new string which is formed from the original string by deleting some (can be none) of the characters without disturbing the relative positions of the remaining characters. (ie, "ace" is a subsequence of "abcde" while "aec" is not).
Example 1:
s = "abc", t = "ahbgdc"
Return true.
Example 2:
s = "axc", t = "ahbgdc"
Return false.
Follow up:
If there are lots of incoming S, say S1, S2, ... , Sk where k >= 1B, and you want to check one by one to see if T has its subsequence. In this scenario, how would you change your code?
Credits:Special thanks to @pbrother for adding this problem and creating all test cases.
|
class Solution:
def isSubsequence(self, s, t):
"""
:type s: str
:type t: str
:rtype: bool
"""
index = 0
ans = True
for i in s:
if i not in t[index:]:
ans = False
break
else:
index += t[index:].index(i) + 1
return ans
|
Given a string s and a string t, check if s is subsequence of t.
You may assume that there is only lower case English letters in both s and t. t is potentially a very long (length ~= 500,000) string, and s is a short string (
A subsequence of a string is a new string which is formed from the original string by deleting some (can be none) of the characters without disturbing the relative positions of the remaining characters. (ie, "ace" is a subsequence of "abcde" while "aec" is not).
Example 1:
s = "abc", t = "ahbgdc"
Return true.
Example 2:
s = "axc", t = "ahbgdc"
Return false.
Follow up:
If there are lots of incoming S, say S1, S2, ... , Sk where k >= 1B, and you want to check one by one to see if T has its subsequence. In this scenario, how would you change your code?
Credits:Special thanks to @pbrother for adding this problem and creating all test cases.
|
class Solution:
def isSubsequence(self, s, t):
"""
:type s: str
:type t: str
:rtype: bool
"""
start = 0
for i in range(len(s)):
index = t[start:].find(s[i])
print(index)
if index == -1:
return False
else:
index += start
start = index+1
return True
|
Given a string s and a string t, check if s is subsequence of t.
You may assume that there is only lower case English letters in both s and t. t is potentially a very long (length ~= 500,000) string, and s is a short string (
A subsequence of a string is a new string which is formed from the original string by deleting some (can be none) of the characters without disturbing the relative positions of the remaining characters. (ie, "ace" is a subsequence of "abcde" while "aec" is not).
Example 1:
s = "abc", t = "ahbgdc"
Return true.
Example 2:
s = "axc", t = "ahbgdc"
Return false.
Follow up:
If there are lots of incoming S, say S1, S2, ... , Sk where k >= 1B, and you want to check one by one to see if T has its subsequence. In this scenario, how would you change your code?
Credits:Special thanks to @pbrother for adding this problem and creating all test cases.
|
class Solution:
def isSubsequence(self, s, t):
t = iter(t)
return all(c in t for c in s)
def isSubsequenceFei(self, s, t):
"""
:type s: str
:type t: str
:rtype: bool
"""
p,l = 0,len(s)
if l == p:
return True
for c_t in t:
if s[p] == c_t:
p += 1
if p == l:
return True
return False
|
Given a string s and a string t, check if s is subsequence of t.
You may assume that there is only lower case English letters in both s and t. t is potentially a very long (length ~= 500,000) string, and s is a short string (
A subsequence of a string is a new string which is formed from the original string by deleting some (can be none) of the characters without disturbing the relative positions of the remaining characters. (ie, "ace" is a subsequence of "abcde" while "aec" is not).
Example 1:
s = "abc", t = "ahbgdc"
Return true.
Example 2:
s = "axc", t = "ahbgdc"
Return false.
Follow up:
If there are lots of incoming S, say S1, S2, ... , Sk where k >= 1B, and you want to check one by one to see if T has its subsequence. In this scenario, how would you change your code?
Credits:Special thanks to @pbrother for adding this problem and creating all test cases.
|
class Solution:
def isSubsequence(self, s, t):
t = iter(t)
return all(c in t for c in s)
|
Given a string s and a string t, check if s is subsequence of t.
You may assume that there is only lower case English letters in both s and t. t is potentially a very long (length ~= 500,000) string, and s is a short string (
A subsequence of a string is a new string which is formed from the original string by deleting some (can be none) of the characters without disturbing the relative positions of the remaining characters. (ie, "ace" is a subsequence of "abcde" while "aec" is not).
Example 1:
s = "abc", t = "ahbgdc"
Return true.
Example 2:
s = "axc", t = "ahbgdc"
Return false.
Follow up:
If there are lots of incoming S, say S1, S2, ... , Sk where k >= 1B, and you want to check one by one to see if T has its subsequence. In this scenario, how would you change your code?
Credits:Special thanks to @pbrother for adding this problem and creating all test cases.
|
class Solution:
def isSubsequence(self, s, t):
"""
:type s: str
:type t: str
:rtype: bool
"""
t= iter(t)
return all(i in t for i in s)
|
Given a string s and a string t, check if s is subsequence of t.
You may assume that there is only lower case English letters in both s and t. t is potentially a very long (length ~= 500,000) string, and s is a short string (
A subsequence of a string is a new string which is formed from the original string by deleting some (can be none) of the characters without disturbing the relative positions of the remaining characters. (ie, "ace" is a subsequence of "abcde" while "aec" is not).
Example 1:
s = "abc", t = "ahbgdc"
Return true.
Example 2:
s = "axc", t = "ahbgdc"
Return false.
Follow up:
If there are lots of incoming S, say S1, S2, ... , Sk where k >= 1B, and you want to check one by one to see if T has its subsequence. In this scenario, how would you change your code?
Credits:Special thanks to @pbrother for adding this problem and creating all test cases.
|
class Solution:
def isSubsequence(self, s, t):
if s == "":
return True
s_num = 0
temp = s[s_num]
for i in t:
if i == temp:
s_num = s_num + 1
if s_num >= len(s):
return True
temp = s[s_num]
return False
|
Given a string s and a string t, check if s is subsequence of t.
You may assume that there is only lower case English letters in both s and t. t is potentially a very long (length ~= 500,000) string, and s is a short string (
A subsequence of a string is a new string which is formed from the original string by deleting some (can be none) of the characters without disturbing the relative positions of the remaining characters. (ie, "ace" is a subsequence of "abcde" while "aec" is not).
Example 1:
s = "abc", t = "ahbgdc"
Return true.
Example 2:
s = "axc", t = "ahbgdc"
Return false.
Follow up:
If there are lots of incoming S, say S1, S2, ... , Sk where k >= 1B, and you want to check one by one to see if T has its subsequence. In this scenario, how would you change your code?
Credits:Special thanks to @pbrother for adding this problem and creating all test cases.
|
class Solution:
def isSubsequence(self, s, t):
iterator = iter(t)
return all(character in iterator for character in s)
|
Given a string s and a string t, check if s is subsequence of t.
You may assume that there is only lower case English letters in both s and t. t is potentially a very long (length ~= 500,000) string, and s is a short string (
A subsequence of a string is a new string which is formed from the original string by deleting some (can be none) of the characters without disturbing the relative positions of the remaining characters. (ie, "ace" is a subsequence of "abcde" while "aec" is not).
Example 1:
s = "abc", t = "ahbgdc"
Return true.
Example 2:
s = "axc", t = "ahbgdc"
Return false.
Follow up:
If there are lots of incoming S, say S1, S2, ... , Sk where k >= 1B, and you want to check one by one to see if T has its subsequence. In this scenario, how would you change your code?
Credits:Special thanks to @pbrother for adding this problem and creating all test cases.
|
class Solution:
def isSubsequence(self, s, t):
if len(s)==0:
return True
if len(s)>len(t):
return False
if s[0] not in t:
return False
else:
return self.isSubsequence(s[1:], t[t.index(s[0])+1:])
"""
:type s: str
:type t: str
:rtype: bool
"""
|
Given a string num representing the digits of a very large integer and an integer k.
You are allowed to swap any two adjacent digits of the integer at most k times.
Return the minimum integer you can obtain also as a string.
Example 1:
Input: num = "4321", k = 4
Output: "1342"
Explanation: The steps to obtain the minimum integer from 4321 with 4 adjacent swaps are shown.
Example 2:
Input: num = "100", k = 1
Output: "010"
Explanation: It's ok for the output to have leading zeros, but the input is guaranteed not to have any leading zeros.
Example 3:
Input: num = "36789", k = 1000
Output: "36789"
Explanation: We can keep the number without any swaps.
Example 4:
Input: num = "22", k = 22
Output: "22"
Example 5:
Input: num = "9438957234785635408", k = 23
Output: "0345989723478563548"
Constraints:
1 <= num.length <= 30000
num contains digits only and doesn't have leading zeros.
1 <= k <= 10^9
|
class Solution:
def minInteger(self, num: str, k: int) -> str:
n=len(num)
if k<=0:
return num
if k>n*(n-1)//2:
return ''.join(sorted(list(num)))
for i in range(10):
idx = num.find(str(i))
if idx>=0 and idx<=k:
return num[idx]+self.minInteger(num[:idx]+num[idx+1:],k-idx)
|
Given a string num representing the digits of a very large integer and an integer k.
You are allowed to swap any two adjacent digits of the integer at most k times.
Return the minimum integer you can obtain also as a string.
Example 1:
Input: num = "4321", k = 4
Output: "1342"
Explanation: The steps to obtain the minimum integer from 4321 with 4 adjacent swaps are shown.
Example 2:
Input: num = "100", k = 1
Output: "010"
Explanation: It's ok for the output to have leading zeros, but the input is guaranteed not to have any leading zeros.
Example 3:
Input: num = "36789", k = 1000
Output: "36789"
Explanation: We can keep the number without any swaps.
Example 4:
Input: num = "22", k = 22
Output: "22"
Example 5:
Input: num = "9438957234785635408", k = 23
Output: "0345989723478563548"
Constraints:
1 <= num.length <= 30000
num contains digits only and doesn't have leading zeros.
1 <= k <= 10^9
|
class Solution:
def minInteger(self, num: str, k: int) -> str:
n=len(num)
if k<=0:
return num
if k>n*(n-1)//2:
return ''.join(sorted(list(num)))
for i in range(10):
idx = num.find(str(i))
if idx>=0 and idx<=k:
return num[idx]+self.minInteger(num[:idx]+num[idx+1:],k-idx)
'''from collections import deque
j=0
arr=[deque() for i in range(10)]
n=len(num)
for i in range(n):
arr[int(num[i])].append(i)
#print(arr)
curr=0
ans=\"\"
def fun(x,y):
l=list(arr[x])
l.append(y)
l.sort()
arr[x]=deque(l)
while k>0 and curr<n:
ch=None
ch_ind=curr
for i in range(0,int(num[curr])):
if arr[i] and arr[i][0]<=k:
ch=i
ch_ind=arr[i][0]
arr[i].popleft()
break
print(arr,ch,ch_ind,curr,k)
if ch:
ans+=str(ch)
k-=(ch_ind-curr)
else:
ans+=num[curr]
curr+=1
print(ans)
return ans'''
|
Given a string num representing the digits of a very large integer and an integer k.
You are allowed to swap any two adjacent digits of the integer at most k times.
Return the minimum integer you can obtain also as a string.
Example 1:
Input: num = "4321", k = 4
Output: "1342"
Explanation: The steps to obtain the minimum integer from 4321 with 4 adjacent swaps are shown.
Example 2:
Input: num = "100", k = 1
Output: "010"
Explanation: It's ok for the output to have leading zeros, but the input is guaranteed not to have any leading zeros.
Example 3:
Input: num = "36789", k = 1000
Output: "36789"
Explanation: We can keep the number without any swaps.
Example 4:
Input: num = "22", k = 22
Output: "22"
Example 5:
Input: num = "9438957234785635408", k = 23
Output: "0345989723478563548"
Constraints:
1 <= num.length <= 30000
num contains digits only and doesn't have leading zeros.
1 <= k <= 10^9
|
class Solution:
def minInteger(self, num: str, k: int) -> str:
rest = num
move = 0
s = ''
for dig in range(10):
count = 0
digC = str(dig)
for i,c in enumerate(rest):
if c == digC:
if k < move + i - count:
rest = rest.replace(digC, '', count)
s += digC * count
remain = k - move
mid = rest[0:remain+1]
while remain:
tmp = min(mid)
for ti, tc in enumerate(mid):
if tc == tmp:
remain -= ti
s += tc
rest = rest.replace(tc, '', 1)
mid = rest[0:remain+1]
break
return s + rest
move += i-count
count += 1
rest = rest.replace(digC, '')
s += digC * count
return s
|
Given a string num representing the digits of a very large integer and an integer k.
You are allowed to swap any two adjacent digits of the integer at most k times.
Return the minimum integer you can obtain also as a string.
Example 1:
Input: num = "4321", k = 4
Output: "1342"
Explanation: The steps to obtain the minimum integer from 4321 with 4 adjacent swaps are shown.
Example 2:
Input: num = "100", k = 1
Output: "010"
Explanation: It's ok for the output to have leading zeros, but the input is guaranteed not to have any leading zeros.
Example 3:
Input: num = "36789", k = 1000
Output: "36789"
Explanation: We can keep the number without any swaps.
Example 4:
Input: num = "22", k = 22
Output: "22"
Example 5:
Input: num = "9438957234785635408", k = 23
Output: "0345989723478563548"
Constraints:
1 <= num.length <= 30000
num contains digits only and doesn't have leading zeros.
1 <= k <= 10^9
|
class Solution:
def minInteger(self, num: str, k: int) -> str:
if k == 0: return num
n = len(num)
if k >= n*(n+1)//2: return ''.join(sorted(num))
for i in range(10):
idx = num.find(str(i))
if 0<=idx<=k:
res = str(i) + self.minInteger(num[:idx]+num[idx+1:], k-idx)
return res
|
Given a string num representing the digits of a very large integer and an integer k.
You are allowed to swap any two adjacent digits of the integer at most k times.
Return the minimum integer you can obtain also as a string.
Example 1:
Input: num = "4321", k = 4
Output: "1342"
Explanation: The steps to obtain the minimum integer from 4321 with 4 adjacent swaps are shown.
Example 2:
Input: num = "100", k = 1
Output: "010"
Explanation: It's ok for the output to have leading zeros, but the input is guaranteed not to have any leading zeros.
Example 3:
Input: num = "36789", k = 1000
Output: "36789"
Explanation: We can keep the number without any swaps.
Example 4:
Input: num = "22", k = 22
Output: "22"
Example 5:
Input: num = "9438957234785635408", k = 23
Output: "0345989723478563548"
Constraints:
1 <= num.length <= 30000
num contains digits only and doesn't have leading zeros.
1 <= k <= 10^9
|
class Solution:
def minInteger(self, num: str, k: int) -> str:
min_num = ''.join(sorted(list(num)))
i = 0
to_find = 0
while num != min_num and k > 0 and i < len(num):
index = num.find(str(to_find), i)
while index != -1:
if index - i <= k:
num = num[:i] + num[index] + num[i:index] + num[index + 1:]
k -= (index - i)
i += 1
to_find = 0
index = num.find(str(to_find), i)
else:
break
to_find += 1
return num
|
Given a string num representing the digits of a very large integer and an integer k.
You are allowed to swap any two adjacent digits of the integer at most k times.
Return the minimum integer you can obtain also as a string.
Example 1:
Input: num = "4321", k = 4
Output: "1342"
Explanation: The steps to obtain the minimum integer from 4321 with 4 adjacent swaps are shown.
Example 2:
Input: num = "100", k = 1
Output: "010"
Explanation: It's ok for the output to have leading zeros, but the input is guaranteed not to have any leading zeros.
Example 3:
Input: num = "36789", k = 1000
Output: "36789"
Explanation: We can keep the number without any swaps.
Example 4:
Input: num = "22", k = 22
Output: "22"
Example 5:
Input: num = "9438957234785635408", k = 23
Output: "0345989723478563548"
Constraints:
1 <= num.length <= 30000
num contains digits only and doesn't have leading zeros.
1 <= k <= 10^9
|
class Solution:
''' greedy approach. start from the left side,
\t and everytime pick the smallest number on the right side that is able to swap, then do the swap
'''
def minInteger(self, num: str, k: int) -> str:
min_num = sorted(list(num))
min_num = ''.join(min_num)
i = 0
to_find = 0
while num != min_num and k > 0 and i < len(num):
indx = num.find(str(to_find), i)
while indx != -1:
if indx - i <= k: # able to swap
num = num[:i] + num[indx] + num[i:indx] + num[indx+1:] # the swap
k -= (indx - i)
i += 1
to_find = 0 # restart the to_find variable
indx = num.find(str(to_find), i)
else:
break
to_find += 1
return num
|
Given a string num representing the digits of a very large integer and an integer k.
You are allowed to swap any two adjacent digits of the integer at most k times.
Return the minimum integer you can obtain also as a string.
Example 1:
Input: num = "4321", k = 4
Output: "1342"
Explanation: The steps to obtain the minimum integer from 4321 with 4 adjacent swaps are shown.
Example 2:
Input: num = "100", k = 1
Output: "010"
Explanation: It's ok for the output to have leading zeros, but the input is guaranteed not to have any leading zeros.
Example 3:
Input: num = "36789", k = 1000
Output: "36789"
Explanation: We can keep the number without any swaps.
Example 4:
Input: num = "22", k = 22
Output: "22"
Example 5:
Input: num = "9438957234785635408", k = 23
Output: "0345989723478563548"
Constraints:
1 <= num.length <= 30000
num contains digits only and doesn't have leading zeros.
1 <= k <= 10^9
|
class Solution:
def minInteger(self, num: str, k: int) -> str:
n = len(num)
if k >= (n-1)*n//2:
return ''.join(sorted(num))
ans = []
while k and num:
for d in '0123456789':
i = num.find(d)
if 0 <= i <= k:
ans.append(d)
num = num[:i] + num[i+1:]
k -= i
break
return ''.join(ans) + num
|
Given a string num representing the digits of a very large integer and an integer k.
You are allowed to swap any two adjacent digits of the integer at most k times.
Return the minimum integer you can obtain also as a string.
Example 1:
Input: num = "4321", k = 4
Output: "1342"
Explanation: The steps to obtain the minimum integer from 4321 with 4 adjacent swaps are shown.
Example 2:
Input: num = "100", k = 1
Output: "010"
Explanation: It's ok for the output to have leading zeros, but the input is guaranteed not to have any leading zeros.
Example 3:
Input: num = "36789", k = 1000
Output: "36789"
Explanation: We can keep the number without any swaps.
Example 4:
Input: num = "22", k = 22
Output: "22"
Example 5:
Input: num = "9438957234785635408", k = 23
Output: "0345989723478563548"
Constraints:
1 <= num.length <= 30000
num contains digits only and doesn't have leading zeros.
1 <= k <= 10^9
|
class Solution:
def minInteger(self, num: str, k: int) -> str:
n = len(num)
if k <= 0:
return num
if k >= (n+1)*n//2:
return ''.join(sorted(num))
for i in range(10):
index = num.find(str(i))
if 0 <= index <= k:
return str(num[index]) + self.minInteger(num[:index]+num[index+1:], k-index)
return num
|
Given a string num representing the digits of a very large integer and an integer k.
You are allowed to swap any two adjacent digits of the integer at most k times.
Return the minimum integer you can obtain also as a string.
Example 1:
Input: num = "4321", k = 4
Output: "1342"
Explanation: The steps to obtain the minimum integer from 4321 with 4 adjacent swaps are shown.
Example 2:
Input: num = "100", k = 1
Output: "010"
Explanation: It's ok for the output to have leading zeros, but the input is guaranteed not to have any leading zeros.
Example 3:
Input: num = "36789", k = 1000
Output: "36789"
Explanation: We can keep the number without any swaps.
Example 4:
Input: num = "22", k = 22
Output: "22"
Example 5:
Input: num = "9438957234785635408", k = 23
Output: "0345989723478563548"
Constraints:
1 <= num.length <= 30000
num contains digits only and doesn't have leading zeros.
1 <= k <= 10^9
|
class Solution:
def minInteger(self, num: str, k: int) -> str:
if k <= 0:
return num
for i in range(10):
ind = num.find(str(i))
if 0 <= ind <= k:
return str(num[ind]) + self.minInteger(num[0:ind] + num[ind+1:], k - ind)
return num
|
Given a string num representing the digits of a very large integer and an integer k.
You are allowed to swap any two adjacent digits of the integer at most k times.
Return the minimum integer you can obtain also as a string.
Example 1:
Input: num = "4321", k = 4
Output: "1342"
Explanation: The steps to obtain the minimum integer from 4321 with 4 adjacent swaps are shown.
Example 2:
Input: num = "100", k = 1
Output: "010"
Explanation: It's ok for the output to have leading zeros, but the input is guaranteed not to have any leading zeros.
Example 3:
Input: num = "36789", k = 1000
Output: "36789"
Explanation: We can keep the number without any swaps.
Example 4:
Input: num = "22", k = 22
Output: "22"
Example 5:
Input: num = "9438957234785635408", k = 23
Output: "0345989723478563548"
Constraints:
1 <= num.length <= 30000
num contains digits only and doesn't have leading zeros.
1 <= k <= 10^9
|
class Solution:
def minInteger(self, num: str, k: int) -> str:
D = 10
ans = num
for d in range(D):
i = num.find(str(d))
if i < 0:
continue
cost = i
if cost > k:
continue
if i == 0:
return num[i] + self.minInteger(num[i + 1:], k)
return num[i] + self.minInteger(num[:i] + num[i + 1:], k - cost)
return num
|
Given a string and a string dictionary, find the longest string in the dictionary that can be formed by deleting some characters of the given string. If there are more than one possible results, return the longest word with the smallest lexicographical order. If there is no possible result, return the empty string.
Example 1:
Input:
s = "abpcplea", d = ["ale","apple","monkey","plea"]
Output:
"apple"
Example 2:
Input:
s = "abpcplea", d = ["a","b","c"]
Output:
"a"
Note:
All the strings in the input will only contain lower-case letters.
The size of the dictionary won't exceed 1,000.
The length of all the strings in the input won't exceed 1,000.
|
class Solution:
def findLongestWord(self, s, d):
"""
:type s: str
:type d: List[str]
:rtype: str
"""
result = ''
for word in d:
lo = 0
for l in word:
lo = s.find(l, lo)+1
if lo == 0:
break
if lo > 0 and len(word) >= len(result):
if len(word) == len(result):
result = word if word < result else result
else:
result = word
return result
|
Given a string and a string dictionary, find the longest string in the dictionary that can be formed by deleting some characters of the given string. If there are more than one possible results, return the longest word with the smallest lexicographical order. If there is no possible result, return the empty string.
Example 1:
Input:
s = "abpcplea", d = ["ale","apple","monkey","plea"]
Output:
"apple"
Example 2:
Input:
s = "abpcplea", d = ["a","b","c"]
Output:
"a"
Note:
All the strings in the input will only contain lower-case letters.
The size of the dictionary won't exceed 1,000.
The length of all the strings in the input won't exceed 1,000.
|
class Solution:
def findLongestWord(self, s, d):
"""
:type s: str
:type d: List[str]
:rtype: str
"""
def find(a,b):
p = 0
for c in b:
p = a.find(c,p) + 1
if p == 0:
return False
return True
ans = ''
for word in d:
if find(s,word):
if len(word) > len(ans):
ans = word
elif len(word) == len(ans) and word < ans:
ans = word
return ans
|
Given a string and a string dictionary, find the longest string in the dictionary that can be formed by deleting some characters of the given string. If there are more than one possible results, return the longest word with the smallest lexicographical order. If there is no possible result, return the empty string.
Example 1:
Input:
s = "abpcplea", d = ["ale","apple","monkey","plea"]
Output:
"apple"
Example 2:
Input:
s = "abpcplea", d = ["a","b","c"]
Output:
"a"
Note:
All the strings in the input will only contain lower-case letters.
The size of the dictionary won't exceed 1,000.
The length of all the strings in the input won't exceed 1,000.
|
class Solution:
def findLongestWord(self, s, d):
"""
:type s: str
:type d: List[str]
:rtype: str
"""
def isMatch(s, w):
index = 0
for c in w:
if c in s:
index = s.find(c, index) + 1
if index == 0:
return False
else:
return False
return True
candidates = []
for word in d:
if isMatch(s, word):
candidates.append(word)
candidates = sorted(candidates, key=lambda w:(len(w), w))
if not candidates:
return ""
else:
maxLen = len(candidates[-1])
for c in candidates:
if len(c) == maxLen:
return c
|
Given a string and a string dictionary, find the longest string in the dictionary that can be formed by deleting some characters of the given string. If there are more than one possible results, return the longest word with the smallest lexicographical order. If there is no possible result, return the empty string.
Example 1:
Input:
s = "abpcplea", d = ["ale","apple","monkey","plea"]
Output:
"apple"
Example 2:
Input:
s = "abpcplea", d = ["a","b","c"]
Output:
"a"
Note:
All the strings in the input will only contain lower-case letters.
The size of the dictionary won't exceed 1,000.
The length of all the strings in the input won't exceed 1,000.
|
class Solution:
def findLongestWord(self, s, d):
"""
:type s: str
:type d: List[str]
:rtype: str
"""
word_dict = collections.defaultdict(list)
for i, word in enumerate(d):
word_dict[word[0]].append((i, word))
res = ""
for c in s:
words_startswithc = word_dict[c]
word_dict[c] = []
for i, word in words_startswithc:
if len(word)==1:
if len(d[i])==len(res):
res = min(res, d[i])
else: res = d[i] if len(d[i])>len(res) else res
else:
word_dict[word[1]].append((i, word[1:]))
return res
|
Given 3 positives numbers a, b and c. Return the minimum flips required in some bits of a and b to make ( a OR b == c ). (bitwise OR operation).
Flip operation consists of change any single bit 1 to 0 or change the bit 0 to 1 in their binary representation.
Example 1:
Input: a = 2, b = 6, c = 5
Output: 3
Explanation: After flips a = 1 , b = 4 , c = 5 such that (a OR b == c)
Example 2:
Input: a = 4, b = 2, c = 7
Output: 1
Example 3:
Input: a = 1, b = 2, c = 3
Output: 0
Constraints:
1 <= a <= 10^9
1 <= b <= 10^9
1 <= c <= 10^9
|
class Solution:
def minFlips(self, a: int, b: int, c: int) -> int:
flips = 0
print(bin(a))
print(bin(b))
print(bin(c))
while a or b or c:
# print(a, b, c)
if c % 2:
if not (a % 2 or b % 2):
flips += 1
else:
flips += a % 2 + b % 2
a //= 2
b //= 2
c //= 2
return flips
|
Given 3 positives numbers a, b and c. Return the minimum flips required in some bits of a and b to make ( a OR b == c ). (bitwise OR operation).
Flip operation consists of change any single bit 1 to 0 or change the bit 0 to 1 in their binary representation.
Example 1:
Input: a = 2, b = 6, c = 5
Output: 3
Explanation: After flips a = 1 , b = 4 , c = 5 such that (a OR b == c)
Example 2:
Input: a = 4, b = 2, c = 7
Output: 1
Example 3:
Input: a = 1, b = 2, c = 3
Output: 0
Constraints:
1 <= a <= 10^9
1 <= b <= 10^9
1 <= c <= 10^9
|
class Solution:
def minFlips(self, a: int, b: int, c: int) -> int:
i, j, k = bin(a)[2:], bin(b)[2:], bin(c)[2:]
maxL = max(len(i), len(j), len(k))
i, j, k = '0' * (maxL - len(i)) + i, '0' * (maxL - len(j)) + j, '0' * (maxL - len(k)) + k
cnt = 0
for x, y, z in zip(i, j, k):
if z == '1' and x == '0' and y == '0':
cnt += 1
if z == '0':
if x == '1':
cnt += 1
if y == '1':
cnt += 1
return cnt
|
Given 3 positives numbers a, b and c. Return the minimum flips required in some bits of a and b to make ( a OR b == c ). (bitwise OR operation).
Flip operation consists of change any single bit 1 to 0 or change the bit 0 to 1 in their binary representation.
Example 1:
Input: a = 2, b = 6, c = 5
Output: 3
Explanation: After flips a = 1 , b = 4 , c = 5 such that (a OR b == c)
Example 2:
Input: a = 4, b = 2, c = 7
Output: 1
Example 3:
Input: a = 1, b = 2, c = 3
Output: 0
Constraints:
1 <= a <= 10^9
1 <= b <= 10^9
1 <= c <= 10^9
|
class Solution:
def minFlips(self, a: int, b: int, c: int) -> int:
count = 0
while a or b or c:
temp = (a & 1) | (b & 1)
if temp != (c & 1):
if c & 1:
count += 1
else:
if a & 1:
count += 1
if b & 1:
count += 1
a = a >> 1
b = b >> 1
c = c >> 1
return count
|
Given 3 positives numbers a, b and c. Return the minimum flips required in some bits of a and b to make ( a OR b == c ). (bitwise OR operation).
Flip operation consists of change any single bit 1 to 0 or change the bit 0 to 1 in their binary representation.
Example 1:
Input: a = 2, b = 6, c = 5
Output: 3
Explanation: After flips a = 1 , b = 4 , c = 5 such that (a OR b == c)
Example 2:
Input: a = 4, b = 2, c = 7
Output: 1
Example 3:
Input: a = 1, b = 2, c = 3
Output: 0
Constraints:
1 <= a <= 10^9
1 <= b <= 10^9
1 <= c <= 10^9
|
class Solution:
def minFlips(self, a: int, b: int, c: int) -> int:
a = bin(a)[2:].zfill(32)
b = bin(b)[2:].zfill(32)
c = bin(c)[2:].zfill(32)
count = 0
for i in range(32):
temp_a = int(a[i])
temp_b = int(b[i])
temp_c = int(c[i])
if temp_a | temp_b != temp_c:
if temp_c == 1:
count += 1
else:
if temp_a == 1:
count += 1
if temp_b == 1:
count += 1
return count
|
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.