在一个由小写英文字母(a-z)组成的字符串中,查找最短子串,其头尾字母相同。输出最左边的该类子串。
作者:互联网
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
String str = scanner.next();
String subStr = str;
for (int i = 0; i < str.length(); i++) {
for (int j = i + 1; j < str.length(); j++) {
if (str.charAt(i) == str.charAt(j)){
if (str.substring(i,j+1).length() < subStr.length()){
subStr = str.substring(i,j+1);
}
}
}
}
System.out.println(subStr);
}
}
标签:子串,小写,英文字母,Scanner,subStr,length,str,scanner,String 来源: https://blog.csdn.net/m0_46653805/article/details/121732484