题目来源:牛客网
万万没想到之聪明的编辑
/**
* className:test1
* Package:PACKAGE_NAME
*
* @Author:swx
* @Create2024/1/1016:33
* @version:1.0
*/
import java.util.Scanner;
public class test1 {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int n = in.nextInt();
while (n>0) {
String a = in.next();
turn(a);
n--;
}
}
private static void turn(String s) {
char[] chars = s.toCharArray();//将传入的String型数据转化为char[];
StringBuilder stringBuilder = new StringBuilder();//创建一个Builder对象接收char[]字符串
int index = 0;
for (int i=0; i<s.length(); i++) {
chars[index] = chars[i];
stringBuilder.append(chars[index]);
index++;
// 1:条件一:三个同样的字母连在一起,一定是拼写错误,去掉一个的就好啦:比如 helllo -> hello
if (index>=3 && chars[index-2]==chars[index-1] && chars[index-3]==chars[index-1]) {
stringBuilder.deleteCharAt(index-1);
index--;
}
// 2:条件二:两对一样的字母(AABB型)连在一起,一定是拼写错误,去掉第二对的一个字母就好啦:比如 helloo -> hello
if (index>=4 && chars[index-4] == chars[index-3] && chars[index-2] == chars[index-1]) {
stringBuilder.deleteCharAt(index-1);
index--;
}
}
System.out.println(String.valueOf(stringBuilder));
}
}