美文网首页
Android面试---字符串找到最长的没重复的子串

Android面试---字符串找到最长的没重复的子串

作者: 咚咚_Coding | 来源:发表于2022-02-12 17:14 被阅读0次

    效果

    输入pwwkew,得到  3..data..wke
    输入abcaaabcdbblhkr90 得到 7..data..blhkr90
    

    code

    String input = "abcaaabcdbblhkr90";
    testStr(input);
    public static String testStr(String input) {
        List<Character> arrayList = new ArrayList<>();
        int max = 0; 
        for (int i = 0; i < input.length(); i++) {
            char curr = input.charAt(i);
            char next = 0;
            if (i < input.length() - 1) {
                next = input.charAt(i + 1);
            }
            if (arrayList.contains(curr)) {
                if (curr != next && next != 0) {
                    max = arrayList.size();
                    arrayList.clear();
                    arrayList.add(curr);
                }
            } else {
                arrayList.add(curr);
            }
        }
        max = Math.max(max, arrayList.size());
        StringBuilder stringBuilder = new StringBuilder();
        System.out.println(max);
        for (Character data : arrayList) {
            stringBuilder.append(data);
        }
        System.out.println(max + "..data.." + stringBuilder.toString());
        return stringBuilder.toString();
    }

    相关文章

      网友评论

          本文标题:Android面试---字符串找到最长的没重复的子串

          本文链接:https://www.haomeiwen.com/subject/bqxxlrtx.html