您的位置:首页 > 其它

(1) 找出两个字符串的最大子串

2016-07-31 14:38 267 查看
package com.legend.debug;

/**
* Created by Legend on 2016/7/31.
*/
class StringOperation {
private String strA = "";
private String strB = "";
private String[] strarrMax = {"", ""};
public StringOperation(String strA, String strB) {
this.strA = strA;
this.strB = strB;
}
public void getMaxSubString() {
String strLong = this.strA.length() > this.strB.length() ? this.strA : this.strB;
String strShort = this.strA.equals(strLong) ? this.strB : this.strA;
int intMaxStringCount = 1;
final int FIRST_NUMBER = 0;
for (int i = 0; i < strShort.length(); i++) {
int intStringNumber = strShort.length() - i;
for (int j = strShort.length(); j > strShort.length() - intStringNumber; j--) {
if (strLong.contains(strShort.substring(i, j))) {
if (strShort.substring(i, j).length() > this.strarrMax[FIRST_NUMBER].length()) {
this.strarrMax[FIRST_NUMBER] = strShort.substring(i, j);
} else if (strShort.substring(i, j).length() == this.strarrMax[FIRST_NUMBER].length()) {
this.strarrMax[intMaxStringCount++] = strShort.substring(i, j);
if (intMaxStringCount == strarrMax.length) {
System.out.println("the same max substring is too much.");
}
}
}
}
}
}
public void print() {
for (int k = 0; k < this.strarrMax.length; k++) {
System.out.println(this.strarrMax[k]);
}
}
}
public class StringDemo {
public static void main(String[] args) {
/**
* sadfcctvghjkl
* zxcctvcv 0-7
* 0  length length-1 length-2 length-3 length-4 length-5 length-6 length-7
* 1  length length-1 length-2 length-3 length-4 length-5 length-6
* 2  length length-1 length-2 length-3 length-4 length-5
* 3  length length-1 length-2 length-3 length-4
* 4                           length-3
* 5                  length-2
* 6         length-1
* 7  length
*/
String strA = "wsadcctvxghjklseasdpqwert";
String strB = "zxcctvxcvxxsdseasdqwert";
StringOperation stringOperation = new StringOperation(strA, strB);
stringOperation.getMaxSubString();
stringOperation.print();
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐