- Decide the key that represents the information needed later.
- Update its count or stored state while scanning the input.
- Use constant-time expected lookups to detect matches or assemble the result.
Code notes
- 101 lines of C++ from the credited upstream file 3435.cpp.
- The implementation visibly relies on sequence storage, hash lookup, ordered lookup.
- 12 loop blocks detected.
Complexity
Expected hash operations are constant time, but the surrounding scan and the number of stored keys determine total work and memory.
Check the problem constraints before deciding whether this complexity will pass.
Use this to learn the idea, then write your own version.
1enum class State { kInit, kVisiting, kVisited };2 3class Solution {4 public:5 vector<vector<int>> supersequences(vector<string>& words) {6 vector<vector<int>> ans;7 const vector<pair<int, int>> edges = getEdges(words);8 const vector<int> nodes = getNodes(edges);9 const vector<int> letterToIndex = getLetterToIndex(nodes);10 vector<vector<int>> graph(nodes.size());11 12 for (const auto& [u, v] : edges)13 graph[letterToIndex[u]].push_back(letterToIndex[v]);14 15 for (const auto& doubledSubset : getMinimumSubsets(graph)) {16 vector<int> freq(26);17 for (const int letter : nodes)18 freq[letter] = 1;19 for (const int index : doubledSubset)20 freq[nodes[index]] = 2;21 ans.push_back(freq);22 }23 24 return ans;25 }26 27 private:28 29 30 vector<vector<int>> getMinimumSubsets(const vector<vector<int>>& graph) {31 const int n = graph.size();32 vector<vector<int>> res;33 for (int subsetSize = 0; subsetSize <= n; ++subsetSize) {34 vector<bool> combination(n);35 fill(combination.end() - subsetSize, combination.end(), true);36 do {37 vector<int> doubledSubset;38 for (int i = 0; i < n; ++i)39 if (combination[i])40 doubledSubset.push_back(i);41 if (!hasCycleSkipping(graph,42 {doubledSubset.begin(), doubledSubset.end()}))43 res.push_back(doubledSubset);44 } while (next_permutation(combination.begin(), combination.end()));45 if (!res.empty())46 return res;47 }48 return res;49 }50 51 52 53 bool hasCycleSkipping(const vector<vector<int>>& graph,54 const unordered_set<int>& doubledSubset) {55 vector<State> states(graph.size());56 for (int i = 0; i < graph.size(); ++i)57 if (hasCycle(graph, i, states, doubledSubset))58 return true;59 return false;60 }61 62 bool hasCycle(const vector<vector<int>>& graph, int u, vector<State>& states,63 const unordered_set<int>& doubledSubset) {64 if (states[u] == State::kVisiting)65 return true;66 if (states[u] == State::kVisited)67 return false;68 states[u] = State::kVisiting;69 if (!doubledSubset.contains(u))70 for (const int v : graph[u])71 if (!doubledSubset.contains(v) &&72 hasCycle(graph, v, states, doubledSubset))73 return true;74 states[u] = State::kVisited;75 return false;76 }77 78 vector<pair<int, int>> getEdges(const vector<string>& words) {79 vector<pair<int, int>> edges;80 for (const string& word : words)81 edges.push_back({word[0] - 'a', word[1] - 'a'});82 return edges;83 }84 85 vector<int> getNodes(const vector<pair<int, int>>& edges) {86 set<int> nodes;87 for (const auto& [u, v] : edges) {88 nodes.insert(u);89 nodes.insert(v);90 }91 return {nodes.begin(), nodes.end()};92 }93 94 vector<int> getLetterToIndex(const vector<int>& nodes) {95 vector<int> letterToIndex(26);96 for (int i = 0; i < nodes.size(); ++i)97 letterToIndex[nodes[i]] = i;98 return letterToIndex;99 }100};101