美文网首页
79. 最长公共子串

79. 最长公共子串

作者: 6默默Welsh | 来源:发表于2018-05-09 13:12 被阅读24次

描述

给出两个字符串,找到最长公共子串,并返回其长度。
子串的字符应该连续的出现在原字符串中,这与子序列有所不同。

样例

给出A=“ABCD”,B=“CBCE”,返回 2

挑战

O(n x m) time and memory.

思路
比较简单,

代码

  1. 暴力解法,枚举出所有的子串,i 和 j 代表子串的起点,current 代表子串的长度, i + current 和 j + current 代表子串的终点
public int longestCommonSubstring(String A, String B) {
    int n = A.length();
    int m = B.length();
    int max = 0;
        
    for (int i = 0; i < n; i++) {
        for (int j = 0; j < m; j++) {
            // current 代表当前子串长度
            int current = 0;
            // while 循环的时间复杂度为 O(n)
            while (i + current < n && j + current < m && A.charAt(i + current) == B.charAt(j + current))
                current++;
            max = Math.max(max, current);
        }
    }
    return max;
}
  1. dp 但并不是最优
public class Solution {
    /**
     * @param A, B: Two string.
     * @return: the length of the longest common substring.
     */
    public int longestCommonSubstring(String A, String B) {
        // f[i][j] 代表以当前 i - 1 和 j -1 为终点的两个子串中同样以以 i 和 j 为终点的最长公共子串长度
        // 注意 f[i][j] 描述的最长公共子串的终点也是 i - 1 和 j - 1
        int n = A.length();
        int m = B.length();
        int[][] f = new int[n + 1][m + 1];
        
        // initialize: f[i][j] is 0 by default
        
        // function: f[i][j] = f[i - 1][j - 1] + 1 or 0
        // i 和 j 分别代表子串的终点
        for (int i = 0; i < n; i++) {
            for (int j = 0; j < m; j++) {
                if (A.charAt(i) == B.charAt(j)) {
                    f[i+1][j+1] = f[i][j] + 1;
                // 如果终点不相等,以 i j 为终点的公共子串的长度也就变成了 0
                } else {
                    f[i+1][j+1] = 0;
                }
            }
        }
        
        // answer: max{f[i][j]}
        int max = 0;
        for (int i = 0; i <= n; i++) {
            for (int j = 0; j <= m; j++) {
                max = Math.max(max, f[i][j]);
            }
        }
        
        return max;
    }
}

相关文章

  • 最长公共子序列和最长公共子串

    最长公共子序列和最长公共子串区别 最长公共子串(Longest CommonSubstring)和最长公共子序列(...

  • 79. 最长公共子串

    描述 给出两个字符串,找到最长公共子串,并返回其长度。子串的字符应该连续的出现在原字符串中,这与子序列有所不同。 ...

  • 子串 子序列 总结

    最长公共子串 子串的要求比子序列严格,所以可以讨论子串的终点 最长公共子序列 DP解 递归+memo 最长公共回文...

  • LCS问题

    LCS问题包括最长公共子序列和最长公共子串,其中,最长公共子串要求必须连续。 对于二者的求解方式 最长公共子序列:...

  • lintcode 79. 最长公共子串

    难度:中等 1. Description 2. Solution 动态规划入门题 python3 3. Refer...

  • 最长公共 / 对称字串

    求最长对称字串是求最长公共子串的变形.. (๑˘ ˘๑) 最长公共子串 Longest Common Subseq...

  • 字符串算法

    最长公共前缀 最长回文串 最长回文子序列 最长公共子串 反转单词顺序列 反转字符串 字符串转数字 IP-int互转

  • 06-18:刷题综合一:动态规划

    1、最长公共子串 牛客网:最长公共子串 https://www.nowcoder.com/practice/f33...

  • 每天一道算法题18

    【最长公共子序列,子串】给定两个字符串上str1 和 str2, 求两个字符的最长公共子序列和最长公共子串。 最长...

  • 两个字符串的最长公共子串

    最长公共子串(Longest Common Substring)与最长公共子序列(Longest Common S...

网友评论

      本文标题:79. 最长公共子串

      本文链接:https://www.haomeiwen.com/subject/eaunrftx.html