839. Short Encoding of Words
MediumSolutions in Python, Java, C++, TypeScript
Read the full problem statement on LeetCode and try it first. Below is how to approach it in an interview, followed by a complete answer in each language.
Approach
The problem asks for the length of the shortest possible reference string s that can encode a given list of words. The encoding requires that for each word, it must appear as a substring in s ending just before a '#' character.
Solution
Short Encoding of Words in Python
class Solution:
def minimumLengthEncoding(self, words: List[str]) -> int:
# Create a set from the input list to handle duplicates automatically.
good_words = set(words)
# Iterate through each word in the original list.
for word in words:
# We only need to process words that are potential candidates.
# If `word` was already removed because it's a suffix of another word,
# we can skip it.
if word in good_words:
# For each word, remove all its proper suffixes from the set.
# A proper suffix is a suffix that is not the word itself.
# Example: for "time", suffixes are "ime", "me", "e".
# If "me" is also in the initial words list, it will be removed
# because "time#" already encodes "me".
for i in range(1, len(word)):
good_words.discard(word[i:])
# The set 'good_words' now contains only the words that are not suffixes
# of any other word. Calculate the total length.
# For each word, its length + 1 (for the '#' character).
return sum(len(w) + 1 for w in good_words)
Short Encoding of Words in Java
import java.util.Arrays;
import java.util.HashSet;
import java.util.Set;
class Solution {
public int minimumLengthEncoding(String[] words) {
// Use a HashSet to store the words. This automatically handles duplicates
// and provides efficient removal.
Set<String> s = new HashSet<>(Arrays.asList(words));
// Iterate through each word in the original array.
for (String word : words) {
// We only need to process words that are potential candidates,
// which are those currently in the set.
if (s.contains(word)) {
// For each word, remove all its proper suffixes from the set.
// A proper suffix is any substring starting from index 1 to the end.
for (int i = 1; i < word.length(); i++) {
// Get the suffix starting from index i.
String suffix = word.substring(i);
// If this suffix exists in our set, remove it.
// This is because the longer word 'word' covers this suffix.
s.remove(suffix);
}
}
}
// The set 's' now contains only words that are not suffixes of any other word.
// Calculate the length of the encoding string.
int totalLength = 0;
for (String word : s) {
// Add the length of the word plus 1 for the '#' character.
totalLength += word.length() + 1;
}
return totalLength;
}
}
Short Encoding of Words in C++
class Solution {
public:
int minimumLengthEncoding(vector<string>& words) {
// Use an unordered_set to store unique words for efficient lookup and removal.
// Initialize the set with all words from the input vector.
unordered_set<string> s(words.begin(), words.end());
// Iterate through each word in the original vector.
for (const string& word : words) {
// If the word still exists in our set (it might have been removed as a suffix),
// proceed to remove its own suffixes.
if (s.count(word)) {
// For each word, remove all its proper suffixes from the set.
// A proper suffix is any substring starting from index 1 to the end.
for (int i = 1; i < word.length(); ++i) {
// Get the suffix starting from index i.
// The substr(i) function gets the substring from index i to the end.
s.erase(word.substr(i));
}
}
}
// The set 's' now contains only the words that are not suffixes of any other word.
// Calculate the length of the encoding string.
int totalLength = 0;
for (const string& word : s) {
// Add the length of the word plus 1 for the '#' character.
totalLength += word.length() + 1;
}
return totalLength;
}
};
Short Encoding of Words in TypeScript
function minimumLengthEncoding(words: string[]): number {
// Use a Set to store unique words for efficient lookup and removal.
const s = new Set(words);
// Iterate through each word in the original array.
for (const word of words) {
// We only need to process words that are potential candidates.
if (s.has(word)) {
// For each word, remove all its proper suffixes from the set.
for (let i = 1; i < word.length; i++) {
// Get the suffix starting from index i.
const suffix = word.substring(i);
// If this suffix exists in our set, remove it.
s.delete(suffix);
}
}
}
// The set 's' now contains only words that are not suffixes of any other word.
// Calculate the length of the encoding string.
let totalLength = 0;
for (const word of s) {
// Add the length of the word plus 1 for the '#' character.
totalLength += word.length + 1;
}
return totalLength;
};
More Medium LeetCode answers
Preparing for interviews beyond LeetCode? Practise system design, SQL and behavioral questions with an in-browser runner on KatInterview.
LeetCode is a trademark of LeetCode LLC. KatInterview is not affiliated with LeetCode.