Short Encoding of Words

medium array hash set string trie

Problem

An encoding of a list of words is a string s plus an array of indices so each word is the substring of s up to the next '#'. Find the minimum length of such an encoding string.

Inputwords = ["time", "me", "bell"]
Output10
"time#bell#" works: "me" is a suffix of "time", so it's free. Length = 4 + 1 + 4 + 1 = 10.

def minimum_length_encoding(words):
    s = set(words)
    for w in words:
        for k in range(1, len(w)):
            s.discard(w[k:])
    return sum(len(w) + 1 for w in s)
function minimumLengthEncoding(words) {
  const s = new Set(words);
  for (const w of words) {
    for (let k = 1; k < w.length; k++) s.delete(w.slice(k));
  }
  let total = 0;
  for (const w of s) total += w.length + 1;
  return total;
}
class Solution {
    public int minimumLengthEncoding(String[] words) {
        Set<String> s = new HashSet<>(Arrays.asList(words));
        for (String w : words) {
            for (int k = 1; k < w.length(); k++) s.remove(w.substring(k));
        }
        int total = 0;
        for (String w : s) total += w.length() + 1;
        return total;
    }
}
class Solution {
public:
    int minimumLengthEncoding(vector<string>& words) {
        unordered_set<string> s(words.begin(), words.end());
        for (auto& w : words) {
            for (int k = 1; k < (int)w.size(); k++) s.erase(w.substr(k));
        }
        int total = 0;
        for (auto& w : s) total += w.size() + 1;
        return total;
    }
};
Time: O(Σ|wᵢ|²) Space: O(Σ|wᵢ|)