java获取两个字符串中最大子串_编程找出两个字符串中最大公共子字符串,如"abccade",...

class Solution {

public String comString(String strA, String strB) {

char[] a = strA.toCharArray();

char[] b = strB.toCharArray();

StringBuilder common =  new StringBuilder();

String preCom = "";

int i = 0, j = 0;

while (i 

// 在b里找一个字符跟a[i]相同,直至找到或到头儿

while (j 

j++;

}

// 回溯存档点

int index = i;

while (i 

common.append(a[i]);

i++;

j++;

}

// 如果这一次找到的字符串比上一次长,更新

if (preCom.length() 

preCom = common.toString();

}

common = new StringBuilder();

// 回溯

i = index + 1;

j = 0;

}

return preCom;

}

} 自己试了几个测试用例倒是过了

71e5ea4b43d3e3b9e0e67ab19461dd81.png


版权声明:本文为weixin_39554775原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。