`{"id":"29413a35-f4f4-4b63-aebd-9017d72798c5","name":"Longest Substring With Exactly K Unique Characters","description":"1. You are given a string(str) and a number K.\r\n2. You have to find length of the longest substring that has exactly k unique characters.\r\n3. If no such substring exists, print \"-1\".","inputFormat":"A string","outputFormat":"A number representing length of the longest K unique characters substring.","constraints":"1 &lt;= length of string &lt;= 10^5\r\n1 &lt;= k &lt;= 26","sampleCode":{"cpp":{"code":"#include \"bits/stdc++.h\"\nusing namespace std;\n\nint longestKSubstr(string s, int k) {\n //Write your code here\n }\n\nint main(){\n string s;cin>>s;\n int k;cin>>k;\n cout << longestKSubstr(s,k) << endl;\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":"easy","sampleInput":"aabcbcdbca\r\n2","sampleOutput":"4\r\n","questionVideo":"https://www.youtube.com/embed/SIh3RfFPQwU?end=39","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":"416afe1d-4136-4969-80ed-efb8eea99c3f","name":"Longest Substring With Exactly K Unique Characters","slug":"longest-substring-with-exactly-k-unique-characters","type":1}],"next":{"id":"0dadb40f-1e04-4d78-b860-b835ee2c1b7b","name":"Longest Substring With Exactly K Unique Characters MCQ","type":0,"slug":"longest-substring-with-exactly-k-unique-characters-mcq"},"prev":{"id":"c5b45c23-f89a-4fb6-bc9f-d9372989814e","name":"Count Of Substrings Having All Unique Characters","type":3,"slug":"count-of-substrings-having-all-unique-characters"}}}`

# Longest Substring With Exactly K Unique Characters

1. You are given a string(str) and a number K. 2. You have to find length of the longest substring that has exactly k unique characters. 3. If no such substring exists, print "-1".

`{"id":"29413a35-f4f4-4b63-aebd-9017d72798c5","name":"Longest Substring With Exactly K Unique Characters","description":"1. You are given a string(str) and a number K.\r\n2. You have to find length of the longest substring that has exactly k unique characters.\r\n3. If no such substring exists, print \"-1\".","inputFormat":"A string","outputFormat":"A number representing length of the longest K unique characters substring.","constraints":"1 &lt;= length of string &lt;= 10^5\r\n1 &lt;= k &lt;= 26","sampleCode":{"cpp":{"code":"#include \"bits/stdc++.h\"\nusing namespace std;\n\nint longestKSubstr(string s, int k) {\n //Write your code here\n }\n\nint main(){\n string s;cin>>s;\n int k;cin>>k;\n cout << longestKSubstr(s,k) << endl;\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":"easy","sampleInput":"aabcbcdbca\r\n2","sampleOutput":"4\r\n","questionVideo":"https://www.youtube.com/embed/SIh3RfFPQwU?end=39","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":"416afe1d-4136-4969-80ed-efb8eea99c3f","name":"Longest Substring With Exactly K Unique Characters","slug":"longest-substring-with-exactly-k-unique-characters","type":1}],"next":{"id":"0dadb40f-1e04-4d78-b860-b835ee2c1b7b","name":"Longest Substring With Exactly K Unique Characters MCQ","type":0,"slug":"longest-substring-with-exactly-k-unique-characters-mcq"},"prev":{"id":"c5b45c23-f89a-4fb6-bc9f-d9372989814e","name":"Count Of Substrings Having All Unique Characters","type":3,"slug":"count-of-substrings-having-all-unique-characters"}}}`

Editor

# Longest Substring With Exactly K Unique Characters

easy

1. You are given a string(str) and a number K. 2. You have to find length of the longest substring that has exactly k unique characters. 3. If no such substring exists, print "-1".

## Constraints

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

## Format

A string

### Output

A number representing length of the longest K unique characters substring.

## 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;}4 ```

Question Video

Discussions

Show Discussion

Related Resources