Guest User

Untitled

a guest
Nov 13th, 2018
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.02 KB | None | 0 0
  1. class Solution
  2. {
  3. public:
  4. string minWindow(string s, string p)
  5. {
  6.  
  7. if (s.size() == 0 || p.size() == 0)
  8. return "";
  9.  
  10. int flag = 0;
  11. unordered_map<char, int> hash_pattern;
  12.  
  13. for (int i = 0; i < p.size(); ++i)
  14. ++hash_pattern[p[i]];
  15.  
  16. int counter = hash_pattern.size();
  17.  
  18. int start = 0;
  19. int end = 0;
  20.  
  21. int wbegin = 0;
  22.  
  23. int wlength;
  24. wlength = INT_MAX;
  25.  
  26. string subs = "";
  27.  
  28. while (end < s.size())
  29. {
  30.  
  31. char found = s[end];
  32. cout << s.substr(start, end - start + 1) << endl;
  33.  
  34. if (hash_pattern.count(found) && hash_pattern[found] > 0)
  35. {
  36.  
  37. --hash_pattern[found];
  38.  
  39. if (hash_pattern[found] == 0)
  40. --counter;
  41. }
  42.  
  43. while (counter == 0)
  44. {
  45.  
  46. if (end - start < wlength)
  47. {
  48.  
  49. flag = 1;
  50. wlength = end - start;
  51. subs = s.substr(start, wlength + 1);
  52. }
  53.  
  54. char reducefromleft = s[start];
  55.  
  56. if (hash_pattern.count(reducefromleft))
  57. {
  58. ++hash_pattern[reducefromleft];
  59.  
  60. ++counter;
  61. }
  62.  
  63. ++start;
  64. }
  65.  
  66. ++end;
  67. }
  68.  
  69. if (flag == 0)
  70. subs = "";
  71.  
  72. return subs;
  73. }
  74. };
Add Comment
Please, Sign In to add comment