Find minimum continuous subsequence tags

Given targetList, a list of strings representing the desired tags, and availableTagList, a list of strings representing the sequence of all available tags. Find the start index and end index of the minimus continuous subsequence containing all the desired tags in any order. If more that one results, return the one with the smaller start index.

Note:

  • No dup tags in targetList.
  • Returned subsequence may include additional tags beyond those in targetList.
  • Consider only alphanumeric characters.
  • Output 0 if no match. 

  • 字符串中等题。Sliding window algorithem + Hash。
  • 思想跟其他滑动窗口题一样,不同的是此题没有给出window size,而是要找最小window。还是对targetList建立hash表,然后扩展window end point去找到能包含所有targetList tags的window。找到之后,再移动window start point。这里跟其他题目不一样的地方是,window size并不固定,而且需要找最小window size,所以在满足count == 0条件下,扩展start point来缩小window,直到当前window已经不能包含所有tags。
 1 //
 2 //  main.cpp
 3 //  LeetCode
 4 //
 5 //  Created by Hao on 2017/3/16.
 6 //  Copyright © 2017年 Hao. All rights reserved.
 7 //
 8 
 9 #include <iostream>
10 #include <vector>
11 #include <unordered_map>
12 using namespace std;
13 
14 class Solution {
15 public:
16     vector<int> subSequenceTags(vector<string> targetList, vector<string> availableTagList) {
17         vector<int> vResult;
18         
19         // corner case
20         if (targetList.empty() || availableTagList.empty()) {
21             vResult.push_back(0);
22             return vResult;
23         }
24         
25         unordered_map<string, int>  hash;
26         
27         for (auto & it : targetList)
28             ++ hash[it];
29         
30         // window start/end point, hit count ( sum { positive hash[i] } ), min result length, result start index
31         int left = 0, right = 0, count = targetList.size(), len = INT_MAX, start = 0;
32         
33         while (right < availableTagList.size()) {
34             if (hash[availableTagList.at(right)] > 0) // could use == 1 as well coz that no dup in targetList
35                 -- count;
36             
37             -- hash[availableTagList.at(right)];
38             
39             ++ right;
40             
41             // continue move window start point rightward to minimize window until condition not valid
42             while (0 == count) {
43                 if (right - left < len) {
44                     len = right - left;
45                     start = left;
46                 }
47                 
48                 if (hash[availableTagList.at(left)] >= 0) // could change the condition to == 0 if condition for -- count varies
49                     ++ count;
50                 
51                 ++ hash[availableTagList.at(left)];
52                 
53                 ++ left;
54             }
55         }
56 
57         if (len != INT_MAX) {
58             vResult.push_back(start);
59             vResult.push_back(start + len - 1);
60         } else
61             vResult.push_back(0);   // Don't miss the empty result if no match
62             
63         return vResult;
64     }
65 };
66 
67 int main(int argc, char* argv[])
68 {
69     Solution    testSolution;
70     
71     vector<vector<string>>  tInputs = {{"made", "in", "spain"}, {"2abc","bcd", "cab"}, {"in", "the", "spain"}, {"in"}, {}, {"in"}};
72     vector<vector<string>>  aInputs = {{"made", "weather", "forecast", "says", "that", "made", "rain", "in", "spain", "stays"},
73                                         {"dbc", "2abc", "cab", "bcd", "bcb"},
74                                         {"the", "spain", "that", "the", "rain", "in", "spain", "stays", "forecast", "in", "the"},
75                                         {"aa", "bb", "cc"},
76                                         {"aa", "bb", "cc"},
77                                         {}};
78     vector<int>  result;
79     
80     /*
81      5 8
82      1 3
83      3 6
84      0
85      0
86      0
87      */
88     for (auto i = 0; i < tInputs.size(); ++ i) {
89         result = testSolution.subSequenceTags(tInputs[i], aInputs[i]);
90         
91         for (auto it : result)
92             cout << it << " ";
93         cout << endl;
94     }
95     
96     return 0;
97 }
View Code
原文地址:https://www.cnblogs.com/pegasus923/p/8445421.html