题目描述
在一个由小写英文字母(a-z)组成的字符串中,查找最短子串,其头尾字母相同。输出最左边的该类子串
输入说明
输入1行,为待处理字串(长度≤200)。
输出说明
输出1行,为满足条件的子串。
输入样例复制
dfasjdoiasldhlfa
输出样例复制
ldhl
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
String str = sc.next();
String min = str;
String res = "";
int minLen = Integer.MAX_VALUE;
for (int i = 0; i < str.length(); i++) {
boolean flag = true;
for (int j = i + 1; j < str.length(); j++) {
if (str.charAt(i) == str.charAt(j)) {
res = str.substring(i,j + 1);
break;
}
}
if (res.length() < minLen) {
min = res;
minLen = res.length();
}
}
System.out.println(min);
}
}