JAVA基础算法题(四)
        【摘要】 在一个由小写英文字母(a-z)组成的字符串中,查找最长子串,其头尾字母相同,且中间不包含该头尾字母,并输出最左边的该类子串。        输入说明:待处理字串(长度≤ 200)        输出说明:子串        输入样例:adfasjdoiasldhfa        输出样例:fasjdoiasldhlfpublic class basic{    public static ...
    
    
    
    在一个由小写英文字母(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);
    }
    }
        
            【声明】本内容来自华为云开发者社区博主,不代表华为云及华为云开发者社区的观点和立场。转载时必须标注文章的来源(华为云社区)、文章链接、文章作者等基本信息,否则作者和本社区有权追究责任。如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱:
                cloudbbs@huaweicloud.com
                
            
        
        
        
        
        
        
        - 点赞
 - 收藏
 - 关注作者
 
            
           
评论(0)