该程序在Python中找到最长的字母词组子序列的长度
假设我们有两个小写的字符串S和T,我们必须找到最长的字谜子序列的长度。
因此,如果输入类似于S=“helloworld”,T=“hellorld”,则输出将为8
为了解决这个问题,我们将遵循以下步骤-
c:=新映射,d:=新映射
对于介于0到a大小的i,执行
c[a[i]]:=1
c[a[i]]:=c[a[i]]+1
如果c中的a[i],则
除此以外,
对于0到b大小的i,执行
d[b[i]]:=1
d[b[i]]:=d[b[i]]+1
如果d中的b[i],则
除此以外,
res:=0
对于c中的每个ch,
res:=res+c[ch]和d[ch]的最小值
如果d[ch]>0,则
返回资源
让我们看下面的实现以更好地理解-
示例
class Solution:
   def solve(self, a, b):
      c, d = {}, {}
      for i in range(len(a)):
         if a[i] in c:
            c[a[i]] += 1
         else:
            c[a[i]] = 1
      for i in range(len(b)):
         if b[i] in d:
            d[b[i]] += 1
         else:
            d[b[i]] = 1
      res = 0
      for ch in c:
         if d.get(ch, 0) > 0:
            res += min(c[ch], d[ch])
         return res
ob = Solution()S = "helloworld"
T = "hellorld"
print(ob.solve(S, T))输入值
S = "helloworld", T = "hellorld"
输出结果
1
