fork download
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3.  
  4. string minWindow(string S, string T) {
  5. // Do not write main() function.
  6. // Do not read input, instead use the arguments to the function.
  7. // Do not print the output, instead return values as specified
  8. // Still have a doubt. Checkout www.interviewbit.com/pages/sample_codes/ for more details
  9. unordered_map<char, int> char_count;
  10. int unique_char = 0;
  11. for (int i = 0; i < T.size(); i++) {
  12. if (char_count[T[i]] == 0) {
  13. unique_char++;
  14. }
  15. char_count[T[i]]++;
  16. }
  17. int min_length = INT_MAX;
  18. int left_index = -1;
  19. int right_index = -1;
  20. int j = 0;
  21. for (; j < S.size(); j++) {
  22. if (char_count.find(S[j]) != char_count.end()) {
  23. char_count[S[j]]--;
  24. if (char_count[S[j]] == 0) {
  25. unique_char--;
  26. if (unique_char == 0) {
  27. if ((j + 1) < min_length) {
  28. min_length = j + 1;
  29. left_index = 0;
  30. right_index = j;
  31. }
  32. break;
  33. }
  34. }
  35. }
  36. }
  37. j++;
  38. // cout << "I reached here\n" << std::flush;
  39. int i = 0;
  40. while (i < S.size() && j < S.size()) {
  41. while (char_count.find(S[i]) == char_count.end() && char_count[S[i]] == 0) {
  42. if (char_count.find(S[i]) != char_count.end()) {
  43. char_count[S[i]]++;
  44. cout << "Indie i " << S[i] << " " << char_count[S[i]] << endl;
  45. }
  46. i++;
  47. }
  48. cout << i << " " << S[i] << endl << std::flush;
  49. char_count[S[i]]++;
  50. while (char_count[S[i]] != 0) {
  51. if (char_count.find(S[j]) != char_count.end()) {
  52. char_count[S[j]]--;
  53. cout << S[j] << " " << char_count[S[j]] << endl;
  54. }
  55. // cout << j << " " << S[j] << " " << char_count[S[i]] << endl;
  56. j++;
  57. }
  58. // exit(1);
  59. cout << j << endl << std::flush;
  60. if (char_count[S[i]] != 0) {
  61. break;
  62. }
  63. i++;
  64. if ((j - i) < min_length) {
  65. min_length = j - i;
  66. left_index = i;
  67. right_index = j - 1;
  68. }
  69. }
  70. string sol = "";
  71. if (left_index != -1) {
  72. for (int i = left_index; i <= right_index; i++) {
  73. sol += S[i];
  74. }
  75. }
  76. return sol;
  77. }
  78.  
  79.  
  80.  
  81. int main() {
  82. // your code goes here
  83. string S, T;
  84. cin >> S;
  85. cin >> T;
  86. cout << minWindow(S, T) << endl;
  87. return 0;
  88. }
Success #stdin #stdout 0s 16056KB
stdin
ADOBECODEBANC
ABC
stdout
0 A
B -1
A 0
11
Indie i D 1
Indie i O 1
3 B
11
Indie i E 1
5 C
C 0
13
ADOBEC