`{"id":"99cd2af8-ffe0-42f8-9ba3-91e266bc34be","name":"Longest Substring With At Most K Unique Characters","description":"1. You are given a string(str) and a number K.\r\n2. You have to find the length of longest substring of the given string that contains at most K unique characters.","inputFormat":"A string\r\nA number","outputFormat":"A number representing the length of longest substring with at most K unique characters.","constraints":"1 &lt;= length of string &lt;= 10^5\r\n1 &lt;= K &lt;= 26","sampleCode":{"cpp":{"code":"#include<iostream>\n#include<string>\n#include <bits/stdc++.h>\nusing namespace std;\nint solution(string s, int k) {\n //write your code here\n}\n\n\n\nint main() {\n string str;\n getline(cin, str);\n int k;\n cin >> k;\n cout << solution(str, k);\n\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\t\t\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/qOPxRziTxEQ?end=311","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":"c167c25f-2010-4175-ba4d-07b0a474a1a6","name":"Longest Substring With At Most K Unique Characters","slug":"longest-substring-with-at-most-k-unique-characters","type":1}],"next":{"id":"06e0a00b-5c5c-4116-bc50-f651e949bd18","name":"Longest Substring With At Most K Unique Characters MCQ","type":0,"slug":"longest-substring-with-at-most-k-unique-characters-mcq"},"prev":{"id":"863487a7-9696-4ce4-a4a5-1a87430a7b16","name":"Maximum Consecutive Ones - 2","type":3,"slug":"maximum-consecutive-ones-2"}}}`

# Longest Substring With At Most K Unique Characters

1. You are given a string(str) and a number K. 2. You have to find the length of longest substring of the given string that contains at most K unique characters.

`{"id":"99cd2af8-ffe0-42f8-9ba3-91e266bc34be","name":"Longest Substring With At Most K Unique Characters","description":"1. You are given a string(str) and a number K.\r\n2. You have to find the length of longest substring of the given string that contains at most K unique characters.","inputFormat":"A string\r\nA number","outputFormat":"A number representing the length of longest substring with at most K unique characters.","constraints":"1 &lt;= length of string &lt;= 10^5\r\n1 &lt;= K &lt;= 26","sampleCode":{"cpp":{"code":"#include<iostream>\n#include<string>\n#include <bits/stdc++.h>\nusing namespace std;\nint solution(string s, int k) {\n //write your code here\n}\n\n\n\nint main() {\n string str;\n getline(cin, str);\n int k;\n cin >> k;\n cout << solution(str, k);\n\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\t\t\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/qOPxRziTxEQ?end=311","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":"c167c25f-2010-4175-ba4d-07b0a474a1a6","name":"Longest Substring With At Most K Unique Characters","slug":"longest-substring-with-at-most-k-unique-characters","type":1}],"next":{"id":"06e0a00b-5c5c-4116-bc50-f651e949bd18","name":"Longest Substring With At Most K Unique Characters MCQ","type":0,"slug":"longest-substring-with-at-most-k-unique-characters-mcq"},"prev":{"id":"863487a7-9696-4ce4-a4a5-1a87430a7b16","name":"Maximum Consecutive Ones - 2","type":3,"slug":"maximum-consecutive-ones-2"}}}`

Editor

Loading...

# Longest Substring With At Most K Unique Characters

easy

1. You are given a string(str) and a number K. 2. You have to find the length of longest substring of the given string that contains at most K unique characters.

## Constraints

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

## Format

### Input

A string A number

### Output

A number representing the length of longest substring with at most 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;}4 ```

Question Video

Discussions

Show Discussion

Related Resources