java中如何实现 A= abcd ; B = bcdefg ; 求字符串中连续重复的字符串最多的个数
1个回答
展开全部
public class Test03 {
public static void main(String[] args) {
int max = 0;
String maxStr = "";
String A = "abcd";
String B = "bcdefg";
if (A.length() < B.length()) {
String C = A;
A = B;
B = C;
}
for (int i = 0; i < A.length(); i++) {
for (int j = 0; j < B.length(); j++) {
int n = 0;
try {
if (A.charAt(i) == B.charAt(j)) {
while (A.charAt(i + n) == B.charAt(j + n)) {
n++;
}
}
} catch (StringIndexOutOfBoundsException e) {
if (n > max) {
max = n;
maxStr = A.substring(i, i + max);
}
i++;
j = 0;
continue;
}
}
}
System.out.println("个数:" + max);
System.out.println("字符串:" + maxStr);
}
}
public static void main(String[] args) {
int max = 0;
String maxStr = "";
String A = "abcd";
String B = "bcdefg";
if (A.length() < B.length()) {
String C = A;
A = B;
B = C;
}
for (int i = 0; i < A.length(); i++) {
for (int j = 0; j < B.length(); j++) {
int n = 0;
try {
if (A.charAt(i) == B.charAt(j)) {
while (A.charAt(i + n) == B.charAt(j + n)) {
n++;
}
}
} catch (StringIndexOutOfBoundsException e) {
if (n > max) {
max = n;
maxStr = A.substring(i, i + max);
}
i++;
j = 0;
continue;
}
}
}
System.out.println("个数:" + max);
System.out.println("字符串:" + maxStr);
}
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询