【问题标题】:code to find longest substring with unique K characters not working for all inputs查找具有唯一 K 个字符的最长子字符串的代码不适用于所有输入
【发布时间】:2016-04-20 18:04:00
【问题描述】:

给定一个字符串,""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

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

【问题讨论】:

  • 第一个看起来不错k = 3 egi...或者它可能是deeeegg。两者都是正确的。
  • 第二个应该是eeeegg吧?
  • 是的,我在上面更新了
  • 你可能想在循环中推进i,只要inputArr[i] 不变(顺便说一句,这只会让你更接近,你的算法不正确)
  • 我用相同的输入尝试了链接中的python解决方案。输出为:bcdeeeeeeeegg。你注意到了吗? “bcdeeee”不正确。所以python的解决方案并不完美。

标签: java string algorithm


【解决方案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

【讨论】:

  • 很好的答案!。时间复杂度不是o(n^2)吗?
  • 是的。希望我能想出一个更好的解决方案;)
  • 在python中添加了我的解决方案Here,虽然不是那么优雅:-)
猜你喜欢
  • 2019-09-04
  • 2019-03-11
  • 1970-01-01
  • 2014-02-02
  • 1970-01-01
  • 1970-01-01
  • 2014-07-09
  • 2021-09-14
相关资源
最近更新 更多