Given two strings, find the longest common subsequence (LCS).

Your code should return the length of LCS.

Example

For "ABCD" and "EDCA", the LCS is "A" (or "D""C"), return 1.

For "ABCD" and "EACB", the LCS is "AC", return 2.

分析:

典型的DP。

 public class Solution {
/**
* @param A, B: Two strings.
* @return: The length of longest common subsequence of A and B.
*/
public int longestCommonSubsequence(String str1, String str2) {
if (str1 == null || str2 == null) return ;
int[][] opt = new int[str2.length() + ][str1.length() + ]; for (int j = ; j <= str1.length(); j++) {
for (int i = ; i <= str2.length(); i++) {
if (i == || j == ) {
opt[i][j] = ;
} else if (str2.charAt(i-) == str1.charAt(j-)) {
opt[i][j] = opt[i-][j-] + ;
} else {
opt[i][j] = Math.max(opt[i-][j], opt[i][j-]);
}
}
}
return opt[str2.length()][str1.length()];
}
}

Longest Common Substring

Given two strings, find the longest common substring.

Return the length of it.

Notice

The characters in substring should occur continuously in original string. This is different with subsequence.

Example

Given A = "ABCD", B = "CBCE", return 2.

分析:

从头比到尾呗。

 public class Solution {
/**
* @param A, B: Two string.
* @return: the length of the longest common substring.
*/
public int longestCommonSubstring(String A, String B) {
if (A == null || B == null || A.length() == || B.length() == ) return ;
int max = ;
for (int i = ; i < B.length(); i++) {
for (int j = ; j < A.length(); j++) {
int incr = ;
while (i + incr < B.length() && j + incr < A.length() && (B.charAt(i + incr) == A.charAt(j + incr))) {
incr++;
max = Math.max(max, incr);
}
}
}
return max;
}
}

Longest Common Prefix

Given k strings, find the longest common prefix (LCP).

Example

For strings "ABCD""ABEF" and "ACEF", the LCP is "A"

For strings "ABCDEFG""ABCEFG" and "ABCEFA", the LCP is "ABC"

分析:

取出第一个string和剩余的string相比即可。

 public class Solution {
/**
* @param strs: A list of strings
* @return: The longest common prefix
*/
public String longestCommonPrefix(String[] strs) {
if (strs == null || strs.length == ) return "";
if (strs.length == ) return strs[]; StringBuilder sb = new StringBuilder(); for (int j = ; j < strs[].length(); j++) {
for (int i = ; i < strs.length; i++) {
if (j >= strs[i].length() || strs[i].charAt(j) != strs[].charAt(j)) {
return sb.toString();
}
}
sb.append(strs[].charAt(j));
}
return sb.toString();
}
}

Longest Common Subsequence & Substring & prefix的更多相关文章

  1. lintcode 77.Longest Common Subsequence(最长公共子序列)、79. Longest Common Substring(最长公共子串)

    Longest Common Subsequence最长公共子序列: 每个dp位置表示的是第i.j个字母的最长公共子序列 class Solution { public: int findLength ...

  2. Longest common subsequence(LCS)

    问题 说明该问题在生物学中的实际意义 Biological applications often need to compare the DNA of two (or more) different ...

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

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

  4. Lintcode:Longest Common Subsequence 解题报告

    Longest Common Subsequence 原题链接:http://lintcode.com/zh-cn/problem/longest-common-subsequence/ Given ...

  5. [Algorithms] Longest Common Subsequence

    The Longest Common Subsequence (LCS) problem is as follows: Given two sequences s and t, find the le ...

  6. LintCode Longest Common Subsequence

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

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

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

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

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

  9. Longest Common Subsequence

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

随机推荐

  1. css学习归纳总结

    来源于:trigkit4 css学习归纳总结(一) 选择器的分组 CSS选择器分为 1.群组选择器 如:p, body, img, div{} 2.兄弟选择器 如:p + p { color:#f00 ...

  2. Java基础-设计模式之-代理模式Proxy

    代理模式是对象的结构模式.代理模式给某一个对象提供一个代理对象,并由代理对象控制对原对象的引用. 代理模式是常用的Java 设计模式,它的特征是代理类与委托类有同样的接口,代理类主要负责为委托类预处理 ...

  3. struts2中把action中的值传递到jsp页面的例子

    例子: RegistAction的代码: package com.wss.action; import javax.servlet.http.HttpServletRequest; import or ...

  4. Yii2.0 对数据库 查询的简单操作

    User::find()->all(); 此方法返回所有数据: User::findOne($id); 此方法返回 主键 id=1 的一条数据(举个例子): User::find()->w ...

  5. jquery ajax 应用返回类型是html json

    jquery ajax 例子:    function JudgeUserName()        {            $.ajax({            type:"GET&q ...

  6. Jenkins 搭建U3D自动发布 IOS

    http://www.cnblogs.com/yinghuochong/archive/2013/09/01/3294940.html 1.安装包,工具略过. 2.插件管理 Subversion Pl ...

  7. Redis介绍以及安装(Linux与windows)

    1.liunux系统 redis是当前比较热门的NOSQL系统之一,它是一个key-value存储系统.和Memcached类似,但很大程度补偿了memcached的 不足,它支持存储的value类型 ...

  8. javafx实现饼图统计效果图

  9. 什么是Gn Gi Gb!

    什么是Gn Gi Gb! 2013-08-24 23:58t百既3 | 浏览 776 次 2013-08-25 00:18 #“团队精英计划—个人高质赛ing!# 提问者采纳   答:这三个接口都是G ...

  10. 10 steps to becoming the developer everyone wants

    You thought it was all about programming skills. But you were wrong! Great code is fine, yet command ...