java
import java.util.ArrayList;
import java.util.List;
public class StringChecker {
static String SYMBOLS = "~!@#$%^&*()_+{}|<>?,./";
static List<String> symbolSubstrings = splitStringIntoSubstrings(SYMBOLS, 3);
/**
* 检查字符串是否包含重复或递增的字符
*
* @param str 要检查的字符串
* @return 如果字符串包含重复或递增的字符,则返回true;否则返回false
*/
public static boolean containsRepeatingOrIncrementalChars(String str) {
int length = str.length();
if (length < 3) {
return false;
}
int[] asciiArray = convertToAsciiArray(str);
for (int i = 0; i < asciiArray.length; i++) {
if (i >= asciiArray.length - 2) {
break;
}
int current = asciiArray[i];
int next = asciiArray[i + 1];
int afterNext = asciiArray[i + 2];
// 校验重复或递增的数字或字母
if ((next == current + 1 && afterNext == next + 1) || (current == next && next == afterNext)) {
return true;
}
}
for (String symbol : symbolSubstrings) {
if (str.contains(symbol)) {
return true;
}
}
return false;
}
/**
* 将字符串按指定长度拆分成子串
*
* @param str 要拆分的字符串
* @param substringLength 子串的长度
* @return 拆分后的子串列表
*/
public static List<String> splitStringIntoSubstrings(String str, int substringLength) {
List<String> substrings = new ArrayList<>();
for (int i = 0; i < str.length(); i++) {
if (i + substringLength > str.length()) {
break;
}
int endIndex = Math.min(i + substringLength, str.length());
String substring = str.substring(i, endIndex);
substrings.add(substring);
}
return substrings;
}
/**
* 将字符串转换为ASCII码数组
*
* @param str 要转换的字符串
* @return ASCII码数组
*/
public static int[] convertToAsciiArray(String str) {
int[] asciiArray = new int[str.length()];
for (int i = 0; i < str.length(); i++) {
asciiArray[i] = (int) str.charAt(i);
}
return asciiArray;
}
public static void main(String[] args) {
System.out.println(containsRepeatingOrIncrementalChars("abc3132132")); // true
System.out.println(containsRepeatingOrIncrementalChars("123fasfadfas")); // true
System.out.println(containsRepeatingOrIncrementalChars("!@#dfafa")); // true
System.out.println(containsRepeatingOrIncrementalChars("aabc")); // true
System.out.println(containsRepeatingOrIncrementalChars("fas12@##")); // false
}
}