longest substring with k unique characters

Solutions on MaxInterview for longest substring with k unique characters by the best coders in the world

showing results for - "longest substring with k unique characters"
Luka
01 Mar 2016
1//I will encourage to first understand the problem then implement it on your know and do not just copy paste this code :) Regards
2#include <bits/stdc++.h>
3
4using namespace std;
5
6int main()
7{
8    string str;
9    cout<<"Enter the string :"<<endl;
10    cin>>str;
11    int k;
12    cout<<"Enter the number of unique characters required:"<<endl;
13    cin>>k;
14    map<char,int>mp;
15    int j=0;
16    int i=0;
17    int mx=INT_MIN;
18    int l=str.size();
19    while(j<l)
20    {
21        mp[str[j]]++;
22        if(int(mp.size())<k)
23        {
24            j++;
25        }
26        else if(int(mp.size())==k)
27        {
28            mx=max(mx,j-i+1);
29            j++;
30        }
31        else
32        {
33            while(int(mp.size())>k)
34            {
35                mp[str[i]]--;
36                if(mp[str[i]]==0)
37                {
38                    mp.erase(str[i]);
39                }
40                i++;
41            }
42            j++;
43        }
44    }
45  cout<<mx<<endl;
46
47    return 0;
48}
49
queries leading to this page
given a string s 2c find the length of the longest substring that contains at most 2 distinct characters length of longest substring with at most k distinct charactersgiven a string s 2c find the length of the longest substring t that contains at most k distinct characters find longest substring with identical characters at most k different characterslongest sub string with k distinct characters practice geekforgeekslongest substring with no more than e2 80 98k e2 80 99 distinct characterslongest possible string with k distinct characterslongest unique substringlongest substring with at least k repeating characterslongest substring with at most k distinct characterslongest substring with k repeating characterslength of longest substring such that it has all distinct characterslongest substring with k distinct character given a string 2c find the length of the longest substring t that contains at most k distinct characters longest string with atmost k distinct characterslongest substring with k distinct characters gfg practicelongest substring with k distinct characters 28medium 29given a string find the length of longest substring with unique characters find longest substring with k repeating characterslongest substring with k different characterslongest substring with atmost k unique characters praticelongest substring with at least k distinct charactersgiven a string s 2c find the length of the longest substring with all distinct characters of string s for example 2c for input 22abc 22 2c the output is 3 as 22abc 22 is the longest substring with all distinct characters longest substring with maximum k distinct characters 28medium 29longest substring with k distinct characterslongest substring with at most two distinct characters problemlongest substring with 2 distinct characterslongest substring of 2 unique characterslongest substring having distinct charactersreturn the length of the longest substring consisting of unique characters given a string find the longest substring with unique characterslongest substring with e2 80 98k e2 80 99 distinct characterslongest substring with maximum k distinct charactersfind the longest substring such that all the alphabets in the string are distinctlongest sub string with k distinct characters difficultyfind the longest substring of a string containing k distinct characters hackerrank given a string s 2c find the length of the longest substring with all unique characters longest substring with at most k distinct characters gfglongest substring having k distinct vowelslength of maximum substring with k unique characterslongest substring with k different characters gfgfind the longest substring such that all the alphabets in the substring are distinct the longest substring of a string containing distinct characterslongest substring with distinct characters 2a longest sub string with k distinct characterslongest substring with unique characterslongest substring with k most distictfinding longest substring with k distinct charactersgiven a string s 2c find the length of the longest substring that contains at most k distinct characters longest substring that contains k unique characters1 longest substring with k distinct character find length of the longest substring with all distinct charactersgiven a string s 2c find the length of the longest substring with all distinct characters of string s for example 2c for input 22abca 22 2c the output is 3 as 22abc 22 is the longest substring with all distinct characters longest substring with all distinct characterslongest substring with character count of at least klongest substring with 2 unique characterslongest substring with k distinct characters hackerrankfind the longest substring that contains k unique characterslongest substring with exactly k unique charactersfind longest substring with k distinct characterslongest substring with atmost k unique characterslongest substring with at most k unique characterslongest k unique characters substringlongest substring with k unique characters using hashmaplongest integer substring with distinct charactersfind the length of the longest substring that contains at most k distinct characterslongest sub string with at most k distinct characters difficulty 3a mediumgiven a string find the length of the longest substring with distinct charactersk unique longest substringfind the longest substring with k unique characterslongest substring with at most 2 distinct characters3 find the longest substring of a string containing distinct charactersgiven a string 2c find the length of the longest substring in it with no more than k distinct characters find longest substring with at most k different characters 2alongest sub string with k distinct charactersfind the longest substring with k unique characters in a given stringlongest sub string with k different characterslongest substring with atmost 2 distinct charactersfind length of longest substring with at most k normal characterslongest substring length with k distinct characters221 longest substring with at most k distinct characterslongest substring with e2 80 98k e2 80 99 distinct characters 28medium 29longest substring with exactly k distinct characterslongest substring of a string of distinct k charecterslongest sub string with k distinct characterslongest substring with k unique characters gfg practicelongest subsequence with k unique charactersfind the longest substring of a string containing k distinct characterslongest substring with k unique characterslongest substring with k unique characters