簡體   English   中英

代碼來查找最長的唯一K字符子字符串,不適用於所有輸入

[英]code to find longest substring with unique K characters not working for all inputs

給定一個字符串“ 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

我無法捕獲錯誤所在。 任何幫助將非常感激。 謝謝

您的實現無法按預期方式運行,因為原始的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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM