Complete the JavaScript function countSubstrings
to count the number of unique substrings in a given string that contain exactly k distinct characters. The function takes two parameters:
string
: A string consisting only of lowercase alphabets.
k
: An integer representing the desired number of distinct characters in a substring.
The function should identify all unique substrings in string
that have exactly k distinct characters and return the count of these substrings. Consider substrings as non-unique if they are anagrams of each other (i.e. they contain the same letters in any order). Additionally, it should log the distinct substrings to the console in the following format:
Distinct substrings with exactly <k> distinct characters: [substrings array]