fork download
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3.  
  4.  
  5. void longestKSubstr(string s, int k)
  6. {
  7.  
  8. int n = s.length();
  9. int answer = -1;
  10. for (int i = 0; i < n; i++) {
  11. for (int j = i + 1; j <= n; j++) {
  12. unordered_set<char> distinct(s.begin() + i,
  13. s.begin() + j);
  14. if (distinct.size() == k) {
  15. answer = max(answer, j - i);
  16. }
  17. }
  18. }
  19.  
  20. cout << answer;
  21. }
  22. int main()
  23. {
  24. string s = "aabacbebebe";
  25. int k = 3;
  26.  
  27. // Function Call
  28. longestKSubstr(s, k);
  29. return 0;
  30. }
  31.  
Success #stdin #stdout 0s 5284KB
stdin
Standard input is empty
stdout
7