- Translate each rule into one explicit state update.
- Maintain the invariant after every processed item.
- Return the accumulated state once all relevant input has been handled.
Code notes
- 41 lines of C++ from the credited upstream file 2800.cpp.
- The implementation visibly relies on sequence storage.
- 2 loop blocks detected.
Complexity
Count the number and nesting of passes over the input, then include the maintained containers in the memory estimate.
Check the problem constraints before deciding whether this complexity will pass.
Use this to learn the idea, then write your own version.
1class Solution {2 public:3 string minimumString(string a, string b, string c) {4 const string abc = merge(a, merge(b, c));5 const string acb = merge(a, merge(c, b));6 const string bac = merge(b, merge(a, c));7 const string bca = merge(b, merge(c, a));8 const string cab = merge(c, merge(a, b));9 const string cba = merge(c, merge(b, a));10 return getMin({abc, acb, bac, bca, cab, cba});11 }12 13 private:14 15 string merge(const string& a, const string& b) {16 if (b.find(a) != string::npos) 17 return b;18 for (int i = 0; i < a.length(); ++i) {19 const string aSuffix = a.substr(i);20 const string bPrefix = b.substr(0, min(b.length(), aSuffix.length()));21 if (aSuffix == bPrefix)22 return a + b.substr(bPrefix.length());23 }24 return a + b;25 }26 27 28 string getMin(const vector<string>& words) {29 string res = words[0];30 for (int i = 1; i < words.size(); ++i)31 res = getMin(res, words[i]);32 return res;33 }34 35 36 string getMin(const string& a, const string& b) {37 return (a.length() < b.length() || (a.length() == b.length() && a < b)) ? a38 : b;39 }40};41