JAVA | Trie Solution | HashSet Solution
\n//--------------Trie Solution---------------------\nclass Solution {\n    public int countDistinct(int[] nums, int k, int p) {\n        TrieNode root = new Tr
```\n//--------------Trie Solution---------------------\nclass Solution {\n    public int countDistinct(int[] nums, int k, int p) {\n        TrieNode root = new TrieNode();\n        int res = 0;\n        for(int i=0; i< nums.length; i++) {\n            TrieNode curr = root;\n            int count = 0;\n            for(int j=i; j<nums.length; j++) {\n                if(nums[j]%p==0) count++;\n                if(count>k) break;\n                if(!curr.links.containsKey(nums[j])) {\n                    res++;\n                    TrieNode next = new TrieNode();\n                    curr.links.put(nums[j], next);\n                }\n                curr = curr.links.get(nums[j]);\n            }\n        }\n        return res;\n    }\n    \n    class TrieNode {\n        Map<Integer, TrieNode> links;\n        \n        public TrieNode() {\n            links = new HashMap<>();\n        }\n    }\n}\n\n```\n\n```\n//--------------HashSet Solution---------------------\nclass Solution {\n    public int countDistinct(int[] nums, int k, int p) {\n\t\tSet<String> res = new HashSet<>();\n        for(int i=0; i<nums.length; i++) {\n            StringBuilder sb = new StringBuilder();\n            int count = 0;\n            for(int j=i; j<nums.length; j++) {\n                if(nums[j]%p==0) count++;\n                if(count>k) break; \n                sb.append(nums[j] + ":");\n                res.add(sb.toString());\n            }\n        }\n        return res.size();\n\t}\n}\n```\n
['Trie', 'Java']