码迷,mamicode.com
首页 > 其他好文 > 详细

查找两个字符串a,b中的最长公共子串

时间:2015-08-19 23:49:10      阅读:188      评论:0      收藏:0      [点我收藏+]

标签:

import java.util.Scanner;


public class GetCommonStr {

	public static void main(String[] args) {
		Scanner scan = new Scanner(System.in);
		String str1 = scan.nextLine();
		String str2 = scan.nextLine();
		
		String result = getCommonStr(str1,str2);
		System.out.println(result);
		
	}

	private static String getCommonStr(String str1, String str2) {
		str1 = str1.toLowerCase();  
        str2 = str2.toLowerCase();  
        for (int i = 0; i < str2.length(); i++) {  
            for (int j = 0,k = str2.length()-i; k != str2.length() + 1; j++,k++) {  
                String temp = str2.substring(j, k);  
                if(str1.contains(temp))  
                    return temp;  
            }  
        }  
        return null;  
	}

}

版权声明:本文为博主原创文章,未经博主允许不得转载。

查找两个字符串a,b中的最长公共子串

标签:

原文地址:http://blog.csdn.net/liaction/article/details/47789161

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!