效果
输入pwwkew,得到 3..data..wke
输入abcaaabcdbblhkr90 得到 7..data..blhkr90
code
String input = "abcaaabcdbblhkr90";
testStr(input);
public static String testStr(String input) {
List<Character> arrayList = new ArrayList<>();
int max = 0;
for (int i = 0; i < input.length(); i++) {
char curr = input.charAt(i);
char next = 0;
if (i < input.length() - 1) {
next = input.charAt(i + 1);
}
if (arrayList.contains(curr)) {
if (curr != next && next != 0) {
max = arrayList.size();
arrayList.clear();
arrayList.add(curr);
}
} else {
arrayList.add(curr);
}
}
max = Math.max(max, arrayList.size());
StringBuilder stringBuilder = new StringBuilder();
System.out.println(max);
for (Character data : arrayList) {
stringBuilder.append(data);
}
System.out.println(max + "..data.." + stringBuilder.toString());
return stringBuilder.toString();
}
网友评论