LeetCode 面试经典150题 [32/150 最小覆盖子串]


avatar
GuoYulong 2024-06-24 174

题目描述

给你一个字符串 s 、一个字符串 t 。返回 s 中涵盖 t 所有字符的最小子串。如果 s 中不存在涵盖 t 所有字符的子串,则返回空字符串 "" 。
注意:
对于 t 中重复字符,我们寻找的子字符串中该字符数量必须不少于 t 中该字符数量。
如果 s 中存在这样的子串,我们保证它是唯一的答案。

示例 1:

复制代码
  1. 输入:s = "ADOBECODEBANC", t = "ABC"
  2. 输出:"BANC"
  3. 解释:最小覆盖子串 "BANC" 包含来自字符串 t 'A''B' 'C'

示例 2:

复制代码
  1. 输入:s = "a", t = "a"
  2. 输出:"a"
  3. 解释:整个字符串 s 是最小覆盖子串。

示例 3:

复制代码
  1. 输入: s = "a", t = "aa"
  2. 输出: ""
  3. 解释: t 中两个字符 'a' 均应包含在 s 的子串中,因此没有符合条件的子字符串,返回空字符串。

个人C++解答


屎山代码且超时:
考虑和上一道题相同思路,创建哈希表进行查找,利用滑动窗口依次判断,但是超时了;

GYL
复制代码
  1. class Solution {
  2. public:
  3. string minWindow(string s, string t) {
  4. if (s.size() < t.size())
  5. return "";
  6. if (s.size() == 1 && t.size() == 1) {
  7. if (s.compare(t) == 0)
  8. return s;
  9. else
  10. return "";
  11. }
  12. unordered_map<char, int> myMap;
  13. for (char w : t) myMap[w]++;
  14. int left = 0, right = 0;
  15. bool first = true,find_one = false;
  16. string result = s;
  17. string TempStr = "";
  18. unordered_map TempMap(myMap);
  19. while (right < s.size()) {
  20. if (first) {
  21. if (myMap.find(s[right]) != myMap.end()){
  22. find_one = true;
  23. TempMap[s[right]]--;
  24. }
  25. TempStr.push_back(s[right]);
  26. if (TempMap[s[right]] <= 0)
  27. TempMap.erase(s[right]);
  28. } else {
  29. TempStr.push_back(s[right]);
  30. if (s[right] == s[left - 1]) {
  31. TempMap.clear();
  32. }
  33. }
  34. if (TempMap.empty()) {
  35. result = TempStr.size() <= result.size() ? TempStr : result;
  36. first = false;
  37. for (int i = left; i <= right; i++) {
  38. if (myMap.find(s[i]) != myMap.end()) {
  39. TempMap[s[i]]++;
  40. }
  41. }
  42. while (left < right) {
  43. if (myMap.find(s[left]) != myMap.end()) {
  44. TempMap[s[left]]--;
  45. if (TempMap[s[left]] < myMap[s[left]])
  46. break;
  47. }
  48. left++;
  49. TempStr = TempStr.substr(1);
  50. }
  51. left++;
  52. if (TempStr.size() >= t.size())
  53. result = TempStr.size() <= result.size() ? TempStr : result;
  54. TempStr = TempStr.substr(1);
  55. }
  56. right++;
  57. }
  58. if(first&&!TempMap.empty())
  59. return "";
  60. return result=find_one==true ?result:"";
  61. }
  62. };

GPT优化

复制代码
  1. class Solution {
  2. public:
  3. string minWindow(string s, string t) {
  4. // Edge case: If s is shorter than t, it's impossible to have a valid window
  5. if (s.size() < t.size())
  6. return "";
  7.  
  8. // Count the frequency of characters in t
  9. unordered_map<char, int> t_count;
  10. for (char c : t) {
  11. t_count[c]++;
  12. }
  13.  
  14. // Sliding window variables
  15. unordered_map<char, int> window_count;
  16. int have = 0, need = t_count.size(); // have: number of characters meeting the requirement
  17. int left = 0, right = 0;
  18. int min_length = INT_MAX, min_left = 0;
  19.  
  20. // Expand the window by moving the right pointer
  21. while (right < s.size()) {
  22. char c = s[right];
  23. window_count[c]++;
  24. if (t_count.find(c) != t_count.end() && window_count[c] == t_count[c]) {
  25. have++;
  26. }
  27.  
  28. // Contract the window by moving the left pointer
  29. while (have == need) {
  30. // Update the minimum window
  31. if (right - left + 1 < min_length) {
  32. min_length = right - left + 1;
  33. min_left = left;
  34. }
  35.  
  36. // Remove the leftmost character from the window
  37. window_count[s[left]]--;
  38. if (t_count.find(s[left]) != t_count.end() && window_count[s[left]] < t_count[s[left]]) {
  39. have--;
  40. }
  41. left++;
  42. }
  43. right++;
  44. }
  45.  
  46. // If no valid window was found, return an empty string
  47. if (min_length == INT_MAX) {
  48. return "";
  49. }
  50.  
  51. return s.substr(min_left, min_length);
  52. }
  53. };
  54.  

相关阅读

注意!!!

站点域名更新!!!部分文章图片等由于域名问题无法显示!!!

通知!!!

站点域名更新!!!部分文章图片等由于域名问题无法显示!!!