java - 查找具有唯一 K 个字符的最长子字符串的代码不适用于所有输入

标签 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

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

最佳答案

您的实现没有按预期方式工作,是因为原始的 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

关于java - 查找具有唯一 K 个字符的最长子字符串的代码不适用于所有输入,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36751753/

相关文章:

java - 在 Java 中创建 .obj 文件

c++ - char str[m][n] 和 char *str[] 传递给函数时的区别

c++ - 不旋转包装矩形?

java - 我想在应用程序运行时请求权限 android Marshmallow ..!

java - Java中如何访问ArrayList中存储的值?

python - 根据条件 pandas 数据框列删除字符串

c - 我怎样才能生成这样的 map ?

c++ - 这不应该使用回溯算法吗?

java - 两个骰子相加 3600 万次

php - SQL WHERE 子句 : sort by date for string