Skip to content

Commit

Permalink
Added solution - LeetHub
Browse files Browse the repository at this point in the history
  • Loading branch information
sainikcodes24x7 committed Jul 9, 2023
1 parent 794757d commit 7b56125
Showing 1 changed file with 50 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
//{ Driver Code Starts
//Initial template for C++

#include <bits/stdc++.h>
using namespace std;

// } Driver Code Ends
//User function template for C++

class Solution{
public:
int longestKSubstr(string s, int k) {
// your code here
unordered_map<char,int>mp;
int n=s.size();
int start=0,end=0,ans=-1;
for(end=0;end<n;end++){
mp[s[end]]++;
if(mp.size()>k){
while(start<=end and start<n and mp.size()>k){
mp[s[start]]--;
if(mp[s[start]]==0){
mp.erase(s[start]);
}
start++;
}
}
if(mp.size()==k){
ans=max(ans,end-start+1);
}
}
return ans;
}
};

//{ Driver Code Starts.
int main() {
int t;
cin >> t;
while (t--) {
string s;
cin >> s;
int k;
cin >> k;
Solution ob;
cout << ob.longestKSubstr(s, k) << endl;
}
}

// } Driver Code Ends

0 comments on commit 7b56125

Please sign in to comment.