BM66 最长公共子串

描述

给定两个字符串str1和str2,输出两个字符串的最长公共子串

题目保证str1和str2的最长公共子串存在且唯一。 

数据范围: Java练习题—动态规划(五)_i++
要求: 空间复杂度 Java练习题—动态规划(五)_i++_02,时间复杂度 Java练习题—动态规划(五)_i++_02

示例1

输入:

"1AB2345CD","12345EF"
  • 1.

返回值:

"2345"
  • 1.


Java代码:

import java.util.*;
public class Solution {
    /**
     * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
     *
     * longest common substring
     * @param str1 string字符串 the string
     * @param str2 string字符串 the string
     * @return string字符串
     */
    public String LCS (String str1, String str2) {
        // write code here
        if (str1.length() < 1 || str2.length() < 1) {
            return "";
        }
        // 以i-1结尾的str1和以j-1结尾的str2的最长公共子串长度
        int[][] dp = new int[str1.length() + 1][str2.length() + 1];
        int count = 0;
        int i = 1, j = 1;
        int temp = 0;
        for (i = 1; i < str1.length() + 1; i++) {
            for (j = 1; j < str2.length() + 1; j++) {
                if (str1.charAt(i - 1) == str2.charAt(j - 1)) {
                    dp[i][j] = dp[i - 1][j - 1] + 1;
                } else {
                    dp[i][j] = 0;
                }
                // 获取最长公共子串长度count
                if (count < dp[i][j]) {
                    count = dp[i][j];
                    // 记录此时最后一个字符所在位置
                    temp = i - 1;
                }
            }
        }
        // 对其中一个字符串进行切割,即可获得最终答案
        String result = str1.substring(temp - count + 1, temp + 1);
        return result;
    }
}
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.
  • 27.
  • 28.
  • 29.
  • 30.
  • 31.
  • 32.
  • 33.
  • 34.
  • 35.
  • 36.
  • 37.
  • 38.
  • 39.
  • 40.


import java.util.*;
public class Solution {
    /**
     * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
     *
     * longest common substring
     * @param str1 string字符串 the string
     * @param str2 string字符串 the string
     * @return string字符串
     */
    public String LCS (String str1, String str2) {
        // write code here
        int n = str1.length();
        int m = str2.length();

        String[][] dp = new String[n+1][m+1];

        String max ="";

        for (int i = 0; i < n+1; i++) {
            dp[i][0] = "";
        }
        for (int j = 0; j < m+1; j++) {
            dp[0][j] = "";
        }

        for (int i = 1; i < n + 1; i++) {
            for (int j = 1; j < m + 1; j++) {
                if(str1.charAt(i-1)==str2.charAt(j-1)){
                    dp[i][j] = dp[i-1][j-1] + str1.charAt(i-1);
                }else {
                    dp[i][j] = "";
                }
                
                if(max.length()<dp[i][j].length()){
                    max = dp[i][j];
                }
 
            }
        }

        return max;
    }
}
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.
  • 27.
  • 28.
  • 29.
  • 30.
  • 31.
  • 32.
  • 33.
  • 34.
  • 35.
  • 36.
  • 37.
  • 38.
  • 39.
  • 40.
  • 41.
  • 42.
  • 43.
  • 44.