本帖最后由 绝版坏银 于 2015-6-13 15:20 编辑
在阳哥笔记里有这么一道题,- package com.itheima;
- /*
- * 两个字符串中最大相同的子串
- * "qwerabcdtyuiop"
- * "xcabcdvbn"
- */
- public class Test3 {
- public static void main(String[] args) {
- String s1 = "qwerabcdtyuiop";
- String s2 = "xcabcdvbn";
- System.out.println(getMax(s1, s2));
- }
- private static String getMax(String s1, String s2) {
- String max = null, min = null;
- // 比较两个字符串长度,长的为max,短的为min
- max = (s1.length() > s2.length()) ? s1 : s2;
- min = max.equals(s1) ? s2 : s1;
- for (int i = 0; i < min.length(); i++) {
- for (int a = 0, b = min.length() - i; b != min.length() + 1; a++, b++) {
- String sub = min.substring(a,b);
- if(max.contains(sub)){
- return sub;
- }
- }
- }
- return null;
- }
- }
复制代码 |
|