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.

DP with a 2D array

Time & space: O(m * n)

 class Solution {
public int longestCommonSubsequence(String text1, String text2) {
if (text1 == null || text1.length() == 0 || text2 == null || text2.length() == 0)
return 0;
int[][] dp = new int[text1.length() + 1][text2.length() + 1];
for (int i = 1; i <= text1.length(); i ++) {
for (int j = 1; j <= text2.length(); j ++) {
int val = Math.max(dp[i - 1][j], dp[i][j - 1]);
if (text1.charAt(i - 1) == text2.charAt(j - 1)) {
val = Math.max(val, dp[i - 1][j - 1] + 1);
}
dp[i][j] = val;
}
}
return dp[text1.length()][text2.length()];
}
}

(Skim through) memory optimization, referencing: https://leetcode.com/problems/longest-common-subsequence/discuss/351689/Java-Two-DP-codes-of-O(mn)-and-O(min(m-n))-spaces-w-picture-and-analysis

Obviously, the code in method 1 only needs information of previous row to update current row. So we just use a two-row 2D array to save and update the matching results for chars in s1 and s2.

Note: use k ^ 1 and k ^= 1 to switch between dp[0] (row 0) and dp[1] (row 1).

     public int longestCommonSubsequence(String s1, String s2) {
int m = s1.length(), n = s2.length();
if (m < n) return longestCommonSubsequence(s2, s1);
int[][] dp = new int[2][n + 1];
for (int i = 0, k = 1; i < m; ++i, k ^= 1)
for (int j = 0; j < n; ++j)
if (s1.charAt(i) == s2.charAt(j)) dp[k][j + 1] = 1 + dp[k ^ 1][j];
else dp[k][j + 1] = Math.max(dp[k ^ 1][j + 1], dp[k][j]);
return dp[m % 2][n];
}

Leetcode: Longest Common Subsequence的更多相关文章

  1. LeetCode 1143. Longest Common Subsequence

    原题链接在这里:https://leetcode.com/problems/longest-common-subsequence/ 题目: Given two strings text1 and te ...

  2. 【leetcode】1143. Longest Common Subsequence

    题目如下: Given two strings text1 and text2, return the length of their longest common subsequence. A su ...

  3. 动态规划求最长公共子序列(Longest Common Subsequence, LCS)

    1. 问题描述 子串应该比较好理解,至于什么是子序列,这里给出一个例子:有两个母串 cnblogs belong 比如序列bo, bg, lg在母串cnblogs与belong中都出现过并且出现顺序与 ...

  4. LintCode Longest Common Subsequence

    原题链接在这里:http://www.lintcode.com/en/problem/longest-common-subsequence/ 题目: Given two strings, find t ...

  5. [UCSD白板题] Longest Common Subsequence of Three Sequences

    Problem Introduction In this problem, your goal is to compute the length of a longest common subsequ ...

  6. LCS(Longest Common Subsequence 最长公共子序列)

    最长公共子序列 英文缩写为LCS(Longest Common Subsequence).其定义是,一个序列 S ,如果分别是两个或多个已知序列的子序列,且是所有符合此条件序列中最长的,则 S 称为已 ...

  7. Longest Common Subsequence

    Given two strings, find the longest common subsequence (LCS). Your code should return the length of  ...

  8. Longest Common Subsequence & Substring & prefix

    Given two strings, find the longest common subsequence (LCS). Your code should return the length of  ...

  9. Dynamic Programming | Set 4 (Longest Common Subsequence)

    首先来看什么是最长公共子序列:给定两个序列,找到两个序列中均存在的最长公共子序列的长度.子序列需要以相关的顺序呈现,但不必连续.例如,"abc", "abg", ...

随机推荐

  1. python3 生成二维码并存入word文档

    #二维码的制作与解析 import qrcode,zxing,os s='https:////www.baidu.com/' res=qrcode.make(data=s) res.show() re ...

  2. 0025SpringMVC的几种响应方式

    本文主要涉及到一下几种响应方式: 1.返回字符串2.返回void3.返回ModelAndView4.转发关键字forward和重定向关键字redirect返回字符串5.ajax调用返回json 具体实 ...

  3. 《你说对就队》第九次团队作业:【Beta】Scrum meeting 1

    <你说对就队>第九次团队作业:[Beta]Scrum meeting 1 项目 内容 这个作业属于哪个课程 [教师博客主页链接] 这个作业的要求在哪里 [作业链接地址] 团队名称 < ...

  4. discuz论坛门户资讯入库接口【原创】

    最近想打造一个社区门户站点,所以写了这个入库接口,可以对接数据入库. <?php /* * Discuz x3.2 门户免登陆发布接口 * 2018-08-10 * Copyright 68xi ...

  5. 要求用Windows下批处理和Linux下的shell脚本完成,两文本交替输出

    两个短文件 两个空文件 一空一短 两长

  6. python_requests ~爬虫~小视频~~~

    当一只小小的Py_Spider也有一段时间了, 期间,更多的时间是在爬取图片啊, 文字, 文档这类的东西, 今天突然一时兴起, 来爬一手视频! 所以就找到了远近闻名的六间房(六扇门)哈哈,~~~ 1. ...

  7. python是什么编程语言。

    python是一门动态解释性的强类型定义语言.

  8. UWB DWM1000 开源项目框架 之 温度采集

    在之前博文开源一套uwb 框架,后面几篇博文会基于这个开源框架进行简单开发. 让uwb使用者更清楚了解基于这个basecode 开发工作. 这里所做内容是,采集dwm1000 温度,并发送到另一个节点 ...

  9. debug错误总结

    1, 2,就是一个大括号的问题..让你总是得不了满分..明明和别人的代码差不多. 3,就比如P1914,这种藏坑的题,或者说这一类藏坑的题. 坑是什么呢?就是位数不够往后推的时候.. 你不填坑你就得不 ...

  10. 2:tomcat配置优化

    一.Tomcat配置优化 1.Tomcat配置调优 主要调优内容 增加最大连接数 调整工作模式 启用gzip压缩 调整JVM内存大小 作为Web时,动静分离 合理选择垃圾回收算法 尽量使用较新JDK版 ...