17
社区成员
发帖
与我相关
我的任务
分享
在一个由小写英文字母(a-z)组成的字符串中,查找最长子串,其头尾字母相同,且中间不包含该头尾字母,并输出最左边的该类子串。
输入说明:待处理字串(长度≤ 200)
输出说明:子串
输入样例:adfasjdoiasldhfa
输出样例:fasjdoiasldhlf
public class basic{
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
String str= sc.nextLine();
String result="";
for(int i=0;i<str.length();i++){
int start=i;
int end=str.length()-1;
while(start<end) {
if (str.charAt(start) == str.charAt(end)) {
String newstr = str.substring(start, end+1);
String substr = newstr.substring(1, newstr.length()-1);
if (substr.contains(String.valueOf(newstr.charAt(0)))) {
start++;
break;
} else {
result = result.length() > newstr.length() ? result : newstr;
break;
}
} else {
end--;
}
}
}
System.out.println(result);
}
}