fork download
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3.  
  4. int main()
  5. {
  6. string g;
  7. int n, k;
  8. cin >> n >> k >> g;
  9.  
  10. int count = 0;
  11.  
  12. for (int i = 0; i <= n - 1; i++)
  13. {
  14. unordered_map<int, int> b;
  15.  
  16. for (int j = i; j <= n - 1; j++)
  17. {
  18. int number = g[j] - '0';
  19. b[number] = b[number] + 1;
  20.  
  21. // Check whether substring i...j is valid
  22. int u = 0;
  23.  
  24. for (int i = 0; i <= 9; i++)
  25. {
  26. if (b[i] == k || b[i] == 0)// means single time you get frequency of character which is 'k' and '0' then fine
  27. {
  28. // Valid frequency
  29. }
  30. else // But if you not get then it is wrong means it is not valid substring
  31. {
  32. u = 1;
  33. break;
  34. }
  35. }
  36.  
  37. if (u == 0)
  38. {
  39. count = count + 1;
  40. }
  41. }
  42. }
  43.  
  44. cout << count;
  45.  
  46. return 0;
  47. }
  48.  
Success #stdin #stdout 0s 5316KB
stdin
6
2
110022
stdout
6