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. org.hibernate.PropertyValueException: not-null property references a null or transient value:

    org.hibernate.PropertyValueException: not-null property references a null or transient value: com.bj ...

  2. JSON.NET 简单的使用

    JSON.NET(http://json.codeplex.com/)使用来将.NET中的对象转换为JSON字符串(序列化?),或者将JSON字符串转换为.NET中已有类型的对象(反序列化?) 首先为 ...

  3. Java基础-父类-子类执行顺序

    代码解析 子类 package com; /** * 子类 * @author huage * */ public class Test extends Test1{ public static vo ...

  4. 【Gym 100015B】Ball Painting

    题 There are 2N white balls on a table in two rows, making a nice 2-by-N rectangle. Jon has a big pai ...

  5. 18.Android之SharedPreferences数据存储学习

    SharedPreferences是Android中最容易理解的数据存储技术,实际上SharedPreferences处理的就是一个key-value(键值对)SharedPreferences常用来 ...

  6. BZOJ-3282 Tree Link-Cut-Tree(似乎树链剖分亦可)

    蛋蛋用链剖A的,我写的LCT 3282: Tree Time Limit: 30 Sec Memory Limit: 512 MB Submit: 1241 Solved: 542 [Submit][ ...

  7. Android 设计模式 之 单例模式

    http://blog.csdn.net/fangchongbory/article/details/7734199   目录(?)[+] 单例模式常见情景 首先实现1中的单例模式A 实现2中单例模式 ...

  8. Android自动化测试框架对比

    1.Monkeyrunner:优点:操作最为简单,可以录制测试脚本,可视化操作:缺点:主要生成坐标的自动化操作,移植性不强,功能最为局限:2.Rubotium:主要针对某一个APK进行自动化测试,AP ...

  9. Spring学习5-Spring整合JDBC及其事务处理(注解方式)

    一.整合的步骤   1.步骤一:首先要获得DataSource连接池(推荐使用B方式): 要对数据库执行任何的JDBC操作,需要有一个Connection.在Spring中,Connection对象是 ...

  10. jquery------.mouseover()和.mouseout()的高级效果使用

    index.jsp <div style="width:100%;height:40px;background-color:#aaa;position:absolute;"& ...