link to problem

Description:

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.

Solution:

 class Solution:
def longestCommonSubsequence(self, text1: str, text2: str) -> int: n1 = len(text1)
n2 = len(text2) dp = [[0 for i in range(n2+1)] for j in range(n1+1)]
for i in range(n1):
for j in range(n2):
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[n1-1][n2-1]

Notes:

2-d Dynamic Programming

O(mn)

最新文章

  1. Discuz论坛安全加固浅析
  2. css让元素居中显示
  3. Unity3d《Shader篇》描边
  4. BZOJ3421 : Poi2013 Walk
  5. blend 从无到有系列之添加自定义Rectangle样式指定到资源文件
  6. jvm笔记
  7. phpcms如何嵌套循环
  8. 新建cocos2d-xproject
  9. css2.1实现圆角边框
  10. 如何开发由Create-React-App 引导的应用(三)
  11. 基本MVVM 和 ICommand用法举例(转)
  12. visual studio 中sstrcpy报错的问题
  13. [dev]typeof, offsetof 和container_of
  14. JAVA的单元测试技术
  15. css3图片旋转
  16. Fiddler 简单介绍
  17. 使用Ant发布web应用到tomcat
  18. linux性能分析工具之火焰图
  19. Part 2 - Fundamentals(4-10)
  20. 如何把高版本的sqlserver 还原到低版本的 sqlserver

热门文章

  1. UIgradients – 美丽的UI渐变色分享站 并可转成CSS代码
  2. 后缀数组 poj 3415
  3. Strange Bank(找零问题)
  4. EAC3 Spectral Extension Process
  5. Centos610-oracle 备份和还原
  6. python正则子组匹配
  7. WIN10 设置WEB
  8. Bugku-CTF分析篇-中国菜刀(国产神器)
  9. bugku 前女友
  10. Sass&Scss入门 选择器 混合器 导入 条件判断 迭代