一尘不染

代码以查找对所有输入均无效的具有唯一K字符的最长子字符串

algorithm

给定一个字符串“ aabbcdeeeeggi”且k=3,代码应找到最长的子字符串,最多包含k个唯一字符。对于上述输入,它应该是“ deeeeggi”。

这是针对Python中问题的一种优雅的O(n)解决方案。我正在用Java实现。但是我没有得到所有输入的期望输出。

public class SubStringWithKUniqueCharacters {

    public static void main(String[] args){

        System.out.println(longestSubStringWithUniqueK("aabbcdeeeeggi", 3));
        System.out.println(longestSubStringWithUniqueK("aabbcdeeeeggi", 2));
    }

    public static String longestSubStringWithUniqueK(String input, int k){
        int len = input.length();
        Set<Character> unique = new HashSet<>();

        int i = 0;
        int j = 0;
        int count = 0;
        int maxStartIndex = 0;
        int maxEndIndex  = 0;
        int maxLen = 0;
        char[] inputArr = input.toCharArray();

        while (i<len){

            if (count==k && j -i > maxLen){
                maxStartIndex = i;
                maxEndIndex = j;
                maxLen = maxEndIndex - maxStartIndex;
            }
             if (count<k && j<len){
                if (unique.add(inputArr[j])){
                    count++;
                }
                j++;
            }
            else {
                if (unique.remove(inputArr[i])){
                    count--;
                }
                i++;
            }
        }
         return input.substring(maxStartIndex,maxEndIndex);
    }
}

这是输出:

eeeeggi //correct output
eeeggi //incorrect output

我无法捕获错误所在。任何帮助将非常感激。谢谢


阅读 408

收藏
2020-07-28

共1个答案

一尘不染

您的实现无法按预期方式运行,因为原始的python解决方案存在错误。我对您的代码做了一些修改。希望现在还好:

public class SubStringWithKUniqueCharacters {

    public static void main(String[] args){

        System.out.println(longestSubStringWithUniqueK("aabbcdeeeeggi", 3));
        System.out.println(longestSubStringWithUniqueK("aabbcdeeeeggi", 2));
    }

    public static String longestSubStringWithUniqueK(String input, int k){
        int len = input.length();
        Set<Character> unique = new HashSet<>();

        int i = 0;
        int j = 0;
        int count = 0;
        int maxStartIndex = 0;
        int maxEndIndex  = 0;
        int maxLen = 0;
        char[] inputArr = input.toCharArray();

        while (i<len){

            if (count==k && j -i > maxLen){
                maxStartIndex = i;
                maxEndIndex = j;
                maxLen = maxEndIndex - maxStartIndex;
            }
            // 1. if we reach the end of the string, we're done.
            if (j + 1 > len){
                break;
            }
            // 2. changed to count <= k here
            else if (count<= k && j<len){
                if (unique.add(inputArr[j])){
                    count++;
                }
                j++;
            }
            else {                          
                if (unique.remove(inputArr[i])){
                    // 3. remove all consecutive chars of the same value
                    char c = inputArr[i];  // save as temp char
                    while (inputArr[i] == c)
                    {
                        i++;
                    }
                    count--;                          
                }                
            }
        }
         return input.substring(maxStartIndex,maxEndIndex);
    }
}

现在的输出是:

deeeegg
eeeegg
2020-07-28