用于查找最长子字符串的代码,其中唯一的K字符不适用于所有输入

时间:2016-04-20 18:04:01

标签: java string algorithm

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

Here是针对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

我无法捕捉到bug的位置。任何帮助将非常感激。 感谢

1 个答案:

答案 0 :(得分:1)

你的实现没有像预期的那样工作,因为原来的python解决方案有bug。我对你的代码做了一些修改。希望现在好了:

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