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. 【技术分享】linux各种一句话反弹shell总结——攻击者指定服务端,受害者主机(无公网IP)主动连接攻击者的服务端程序(CC server),开启一个shell交互,就叫反弹shell。

    反弹shell背景: 想要搞清楚这个问题,首先要搞清楚什么是反弹,为什么要反弹.假设我们攻击了一台机器,打开了该机器的一个端口,攻击者在自己的机器去连接目标机器(目标ip:目标机器端口),这是比较常规 ...

  2. gdb调试(一)

    对于gdb是什么,这里就不多说了,只要是程序员一般都听说过,像java开发会用到集成开发工具eclipse,里面调试起来非常方便,全是可视化的,但是如果在linux下编写的c程序,用可视化的调试就没这 ...

  3. [python]赶集网二手房爬虫插件【可用任意扩展】

    最近应一个老铁的要求,人家是搞房产的,所以就写了这个二手房的爬虫,因为初版,所以比较简单,有能力的老铁可用进行扩展. import requests import os   from bs4 impo ...

  4. SVM: 实际中使用SVM的一些问题

    使用SVM包来求θ,选择C与核函数 我们使用已经编写好的软件包(这些软件包效率高,用得多,是经无数人证明已经很好的可以使用的软件包)来求θ,而不是自己去编写软件来求它们(就像我们现在很少编写软件来求x ...

  5. myeclipse修改内存大小不足tomcat内存不足

    myeclipse修改内存大小不足 打开Windows-> Preferences -> Java->Installed JREs 点击右侧的jdk,然后点击“Edit”按钮 Def ...

  6. 复杂Java对象所占内存的大小

    我们在Java单个对象内存布局中讲解了单个简单的Java对象所占内存的大小的计算.那么这篇文章主要是讲解复杂Java对象所占内存大小的计算,我们把继承.复合的对象称为复杂对象 继承对象 class P ...

  7. RCNN,Fast RCNN,Faster RCNN 的前生今世:(2) R- CNN (3,2,1)

    3.三次IOU  2.2次model run  1,一次深度神经网络 rcnn主要作用就是用于物体检测,就是首先通过selective search 选择2000个候选区域,这些区域中有我们需要的所对 ...

  8. 项目架构&架构部署&网站分析&网站优化

    一.架构演变 一个项目至少由三层内容组成:web访问层.数据库层.存储层 初级阶段 单体阶段 常见场景:项目初期 部署特点:所有应用服务都在一台主机 应用特点:开发简单 应用/数据分离阶段 常见场景: ...

  9. Optimize Cube.js Performance with Pre-Aggregations

    转自:https://cube.dev/blog/high-performance-data-analytics-with-cubejs-pre-aggregations/ 可以了解 Pre-Aggr ...

  10. loj #2053 莫队

    \(des\) 存在一个长度为 \(n\) 的数字 \(s\), 一个素数 \(P\) \(m\) 次询问一段区间 \([l, r]\) 内的子串构成的数是 \(P\) 的倍数 \(sol\) 对于一 ...