java获取两个字符串中的最大匹配

又是大众点评的算法工程师题目,大众点评用人要求还是蛮高的,

看看吧:


package excise;

public class Test2 {

	public static void main(String[] args) {

		String s1 = "zhoumeixuabcdefg";
		String s2 = "xuabced";
		String maxsub = getMaxSubstring(s2, s1);
		System.out.println("maxsub=" + maxsub);
	}

	public static String getMaxSubstring(String s1, String s2) {
		String max, min;
		max = s1.length() > s2.length() ? s1 : s2;
		min = max.equals(s1) ? s2 : s1;
		// System.out.println("max="+max+",,min="+min);
		for (int x = 0; x < min.length(); x++) {
			for (int y = 0, z = min.length() - x; z != min.length() + 1; y++, z++) {
				// 获取s2子串
				String temp = min.substring(y, z);
				if (max.contains(temp)) {// s1.indexOf(temp)!=-1
					return temp;
				}
			}
		}
		return null;
	}

}


你可能感兴趣的:(java)