Advertisement
TungstenVn

Untitled

Jan 2nd, 2022
162
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.11 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3.  
  4. // Function
  5. string Minimum_Window(string s, string t)
  6. {
  7.  
  8. int m[256] = { 0 };
  9.  
  10. // Answer
  11. int ans = INT_MAX; // length of ans
  12. int start = 0; // starting index of ans
  13. int count = 0;
  14.  
  15. // creating map
  16. for (int i = 0; i < t.length(); i++) {
  17. if (m[t[i]] == 0)
  18. count++;
  19. m[t[i]]++;
  20. }
  21.  
  22. // References of Window
  23. int i = 0;
  24. int j = 0;
  25.  
  26. // Traversing the window
  27. while (j < s.length()) {
  28. // Calculations
  29. m[s[j]]--;
  30. if (m[s[j]] == 0)
  31. count--;
  32.  
  33. // Condition matching
  34. if (count == 0) {
  35. while (count == 0) {
  36. // Sorting ans
  37. if (ans > j - i + 1) {
  38. ans = min(ans, j - i + 1);
  39. start = i;
  40. }
  41. // Sliding I
  42. // Calculation for removing I
  43.  
  44. m[s[i]]++;
  45. if (m[s[i]] > 0)
  46. count++;
  47.  
  48. i++;
  49. }
  50. }
  51. j++;
  52. }
  53.  
  54. if (ans != INT_MAX)
  55. return s.substr(start, ans);
  56. else
  57. return "-1";
  58. }
  59.  
  60. int main()
  61. {
  62.  
  63. string s = "18881";
  64. string t = "18";
  65.  
  66. cout<<"-->Smallest window that contain all character : "<<endl;
  67. cout << Minimum_Window(s, t).length();
  68. return 0;
  69.  
  70. }
  71.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement