题解 | #最长公共子串#

最长公共子串

http://www.nowcoder.com/practice/f33f5adc55f444baa0e0ca87ad8a6aac

解题思路:下标移动法。

    /**
     * longest common substring
     * @param str1 string字符串 the string
     * @param str2 string字符串 the string
     * @return string字符串
     * 解题思路:下标移动法。
     */
    public String LCS (String str1, String str2) {
        String rs = "";
        for (int i = 0; i < str2.length(); i++) {
            for (int j = i + rs.length(); j <= str2.length(); j++) { // 右边坐标所截取的字符串等于已找到的最长子串的长度
//                System.out.println(small.substring(i, j));
                if(str1.indexOf(str2.substring(i, j)) == -1)
                    break;
                else if(j - i > rs.length()){
                    rs = str2.substring(i, j);
                }
            }
        }
        return rs;
    }
全部评论

相关推荐

评论
1
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务