`{"id":"2cf4b4ad-cf0c-48c3-88af-cb35c0f74618","name":"Count Of Substrings With Exactly K Unique Characters","description":"1. You are given a string(str) and a number K.\r\n2. You have to find the count of valid substrings of the given string.\r\n3. Valid substring is defined as a substring that has exactly K unique characters.","inputFormat":"A string","outputFormat":"A number representing count of substrings with exactly K unique characters.","constraints":"1 &lt;= length of string &lt;= 10^5\r\n1 &lt;= k &lt;= 26","sampleCode":{"cpp":{"code":"#include <bits/stdc++.h>\n\nusing namespace std;\n\nint main() {\n string str;\n cin >> str;\n int k;\n cin >> k;\n // write your code here\n}"},"java":{"code":"import java.util.*;\r\n\r\npublic class Main {\r\n\r\n\tpublic static int solution(String str, int k){\r\n\t\t// write your code here\r\n\r\n\t\treturn 0;\r\n\t}\r\n\t\r\n\tpublic static void main(String[] args) {\r\n\t\tScanner scn = new Scanner(System.in);\r\n String str = scn.next();\r\n int k = scn.nextInt();\r\n\t\tSystem.out.println(solution(str,k));\r\n\t}\r\n\r\n}\r\n"},"python":{"code":""}},"points":10,"difficulty":"medium","sampleInput":"aabcbcdbca\r\n2","sampleOutput":"12\r\n","questionVideo":"https://www.youtube.com/embed/CBSeilNvZHs?end=595","hints":[],"associated":[],"solutionSeen":false,"tags":[],"meta":{"path":[{"id":0,"name":"home"},{"id":"0c54b191-7b99-4f2c-acb3-e7f2ec748b2a","name":"Data Structures and Algorithms","slug":"data-structures-and-algorithms","type":0},{"id":"ec6515dc-6811-4c65-912f-0134b65df120","name":"HashMap and Heap For Intermediate","slug":"hashmap-and-heap-for-intermediate-10002","type":0},{"id":"25841ec9-a7d0-4550-8367-9cd2499802f0","name":"Count Of Substrings With Exactly K Unique Characters","slug":"count-of-substrings-with-exactly-k-unique-characters","type":1}],"next":{"id":"202013a0-f278-4ea3-aa7a-63bac473787e","name":"Count Of Substrings With Exactly K Unique Characters MCQ","type":0,"slug":"count-of-substrings-with-exactly-k-unique-characters-mcq"},"prev":{"id":"0eb28acc-a4d3-4295-a6d5-18481b585686","name":"Longest Substring With Exactly K Unique Characters","type":3,"slug":"longest-substring-with-exactly-k-unique-characters"}}}`

# Count Of Substrings With Exactly K Unique Characters

1. You are given a string(str) and a number K. 2. You have to find the count of valid substrings of the given string. 3. Valid substring is defined as a substring that has exactly K unique characters.

`{"id":"2cf4b4ad-cf0c-48c3-88af-cb35c0f74618","name":"Count Of Substrings With Exactly K Unique Characters","description":"1. You are given a string(str) and a number K.\r\n2. You have to find the count of valid substrings of the given string.\r\n3. Valid substring is defined as a substring that has exactly K unique characters.","inputFormat":"A string","outputFormat":"A number representing count of substrings with exactly K unique characters.","constraints":"1 &lt;= length of string &lt;= 10^5\r\n1 &lt;= k &lt;= 26","sampleCode":{"cpp":{"code":"#include <bits/stdc++.h>\n\nusing namespace std;\n\nint main() {\n string str;\n cin >> str;\n int k;\n cin >> k;\n // write your code here\n}"},"java":{"code":"import java.util.*;\r\n\r\npublic class Main {\r\n\r\n\tpublic static int solution(String str, int k){\r\n\t\t// write your code here\r\n\r\n\t\treturn 0;\r\n\t}\r\n\t\r\n\tpublic static void main(String[] args) {\r\n\t\tScanner scn = new Scanner(System.in);\r\n String str = scn.next();\r\n int k = scn.nextInt();\r\n\t\tSystem.out.println(solution(str,k));\r\n\t}\r\n\r\n}\r\n"},"python":{"code":""}},"points":10,"difficulty":"medium","sampleInput":"aabcbcdbca\r\n2","sampleOutput":"12\r\n","questionVideo":"https://www.youtube.com/embed/CBSeilNvZHs?end=595","hints":[],"associated":[],"solutionSeen":false,"tags":[],"meta":{"path":[{"id":0,"name":"home"},{"id":"0c54b191-7b99-4f2c-acb3-e7f2ec748b2a","name":"Data Structures and Algorithms","slug":"data-structures-and-algorithms","type":0},{"id":"ec6515dc-6811-4c65-912f-0134b65df120","name":"HashMap and Heap For Intermediate","slug":"hashmap-and-heap-for-intermediate-10002","type":0},{"id":"25841ec9-a7d0-4550-8367-9cd2499802f0","name":"Count Of Substrings With Exactly K Unique Characters","slug":"count-of-substrings-with-exactly-k-unique-characters","type":1}],"next":{"id":"202013a0-f278-4ea3-aa7a-63bac473787e","name":"Count Of Substrings With Exactly K Unique Characters MCQ","type":0,"slug":"count-of-substrings-with-exactly-k-unique-characters-mcq"},"prev":{"id":"0eb28acc-a4d3-4295-a6d5-18481b585686","name":"Longest Substring With Exactly K Unique Characters","type":3,"slug":"longest-substring-with-exactly-k-unique-characters"}}}`

Editor

# Count Of Substrings With Exactly K Unique Characters

medium

1. You are given a string(str) and a number K. 2. You have to find the count of valid substrings of the given string. 3. Valid substring is defined as a substring that has exactly K unique characters.

## Constraints

1 <= length of string <= 10^5 1 <= k <= 26

## Format

A string

### Output

A number representing count of substrings with exactly K unique characters.

## Example

Sample Input

```.css-23h8hz{color:inherit;font-size:0.875rem;line-height:1.125rem;letter-spacing:0.016rem;font-weight:var(--chakra-fontWeights-normal);white-space:pre-wrap;}aabcbcdbca 2```

### Sample Output

```.css-3oaykw{color:var(--chakra-colors-active-primary);font-size:0.875rem;line-height:1.125rem;letter-spacing:0.016rem;font-weight:var(--chakra-fontWeights-normal);white-space:pre-wrap;font-family:Monospace;}12 ```

Question Video

Discussions

Show Discussion

Related Resources