Problem solution · C++

Minimum Unique Word Abbreviation

Minimum Unique Word Abbreviation: a C++ solution using direct simulation. Learn the idea, check the complexity, and read the full code, with credit to walkccc LeetCode Solutions.

Technique
Direct simulation
Source
walkccc LeetCode Solutions
Length
85 lines
Start with the idea.

Try the problem first. If you get stuck, read the approach below, then write your own solution. The full code is at the bottom.

Approach

Direct simulation

For Minimum Unique Word Abbreviation, the implementation follows the problem’s operations directly while maintaining only the state needed for the next decision.

  1. Translate each rule into one explicit state update.
  2. Maintain the invariant after every processed item.
  3. Return the accumulated state once all relevant input has been handled.

Code notes

  • 85 lines of C++ from the credited upstream file 411.cpp.
  • The implementation visibly relies on sequence storage.
  • 7 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.

Source

Code and credit

This code comes from walkccc LeetCode Solutions by P.-Y. Chen (walkccc) and is used under the MIT licence.

Full codeMinimum Unique Word Abbreviation · C++C++
Use this to learn the idea, then write your own version.
class Solution { public:  string minAbbreviation(string target, vector<string>& dictionary) {    const int m = target.length();    vector<int> masks;     for (const string& word : dictionary) {      if (word.length() != m)        continue;      masks.push_back(getMask(target, word));    }     if (masks.empty())      return to_string(m);     vector<string> abbrs;     const int maxCand = pow(2, m);    // all the candidate representation of the target    for (int cand = 0; cand < maxCand; ++cand)      // All the masks have at lease one bit different from the candidate.      if (ranges::all_of(masks, [cand](int mask) { return cand & mask; }))        abbrs.push_back(getAbbr(target, cand));     string ans = target;     for (const string& abbr : abbrs)      if (getAbbrLen(abbr) < getAbbrLen(ans))        ans = abbr;     return ans;  }  private:  int getMask(const string& target, const string& word) {    const int m = target.length();    // mask[i] = 0 := target[i] == word[i]    // mask[i] = 1 := target[i] != word[i]    // e.g. target = "apple"    //        word = "blade"    //        mask =  11110    int mask = 0;    for (int i = 0; i < m; ++i)      if (word[i] != target[i])        mask |= 1 << m - 1 - i;    return mask;  }   string getAbbr(const string& target, int cand) {    const int m = target.length();    string abbr;    int replacedCount = 0;    for (int i = 0; i < m; ++i)      if (cand >> m - 1 - i & 1) {        // If cand[i] = 1, `abbr` should show the original character.        if (replacedCount > 0)          abbr += to_string(replacedCount);        abbr += target[i];        replacedCount = 0;      } else {        // If cand[i] = 0, `abbr` can be replaced.        ++replacedCount;      }    if (replacedCount > 0)      abbr += to_string(replacedCount);    return abbr;  }   int getAbbrLen(const string& abbr) {    int abbrLen = 0;    int i = 0;    int j = 0;    while (i < abbr.length()) {      if (isalpha(abbr[j]))        ++j;      else        while (j < abbr.length() && isdigit(abbr[j]))          ++j;      ++abbrLen;      i = j;    }    return abbrLen;  }}; 

Did this explanation save you time? I'm a Grade 11 student building this free library to make difficult algorithms easier to understand.

Buy me a coffee ↗