Amazon - K Distinct SubString

Posted incrediblechangshuo

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Amazon - K Distinct SubString相关的知识,希望对你有一定的参考价值。

技术分享图片

技术分享图片

 

 

public class Main {
    public static void main(String[] args) {
        List<String> list = findSubString("awaglknagawybagwkwagl",4);
        for(String s: list){
            System.out.println(s);
        }
    }
    
    public static List<String> findSubString(String input, int k){
        if(input == null || input.length() == 0 || k <= 0){
            return null;
        }
        Map<String, Integer> map = new HashMap<>();
        List<String> resList = new ArrayList<>();
        for(int i = 0; i<input.length()-k; i++){
            Set<Character> set = new HashSet<>();
            boolean isCharDup = false;
            for(int j=i; j<i+k; j++){
                char c = input.charAt(j);
                if(!set.contains(c)){
                    set.add(c);
                }
                else{
                    isCharDup = true;
                    break;
                }
            }
            if(!isCharDup){
                String subStr = input.substring(i, i+k);
                if(!map.containsKey(subStr)){
                    resList.add(subStr);
                }
                else{
                    map.put(subStr, 1);
                }
            }
        }
        return resList;
    }
}

 

以上是关于Amazon - K Distinct SubString的主要内容,如果未能解决你的问题,请参考以下文章

SPOJ - SUBST1 D - New Distinct Substrings

SPOJ SUBST1 - New Distinct Substrings(后缀数组[不相同的子串的个数])

SPOJ SUBST1 - New Distinct Substrings(后缀数组[不相同的子串的个数])

SPOJ 705 Distinct Substrings(后缀数组)

[LeetCode] 340. Longest Substring with At Most K Distinct Characters

[LeetCode] Longest Substring with At Most K Distinct Characters 最多有K个不同字符的最长子串