Leetcode 820: Short Encoding of Words

grid47
grid47
Exploring patterns and algorithms
Aug 17, 2024 6 min read

A series of words encoded, with the shortest encoding glowing softly as it is created.
Solution to LeetCode 820: Short Encoding of Words Problem

Given an array of words, you are to return the length of the shortest reference string that can encode these words. The reference string consists of the words concatenated with a ‘#’ character at the end, and indices are used to encode the words by identifying their position in the reference string. Each word is encoded by the substring in the reference string that starts at its position and ends at the next ‘#’.
Problem
Approach
Steps
Complexity
Input: The input consists of an array of words, where each word is a string of lowercase letters. Each word in the array has a length between 1 and 7 characters.
Example: Input: words = ["apple", "ple", "le"]
Constraints:
• 1 <= words.length <= 2000
• 1 <= words[i].length <= 7
• words[i] consists of only lowercase letters
Output: Return the length of the shortest reference string that can encode the given words.
Example: Output: 9
Constraints:
• The output should be an integer representing the length of the shortest reference string.
Goal: The goal is to find the shortest encoding of the words using a reference string. To do this, we can eliminate words that are suffixes of other words, and concatenate only the distinct prefixes of the words to form the reference string.
Steps:
• Create a set of words to store unique words.
• For each word, check all possible suffixes and remove them from the set if they are also present.
• Finally, concatenate the remaining words, adding 1 for each '#' delimiter, and return the length of the concatenated string.
Goal: Ensure that the solution handles the constraints efficiently, especially when the number of words and the total length of words increases.
Steps:
• The number of words can be up to 2000.
• Each word has a length of at most 7 characters.
Assumptions:
• All words are lowercase and have lengths between 1 and 7 characters.
• Words in the array are unique.
Input: Input: words = ["apple", "ple", "le"]
Explanation: The word 'ple' is a suffix of 'apple', and 'le' is a suffix of 'ple'. So, we can encode the words with 'apple#', which results in a total length of 9.

Input: Input: words = ["orange", "or", "ange"]
Explanation: Here, 'or' is a prefix of 'orange', and 'ange' is a suffix of 'orange'. The shortest encoding would be 'orange#', resulting in a total length of 7.

Link to LeetCode Lab


LeetCode Solutions Library / DSA Sheets / Course Catalog
comments powered by Disqus