Skip to main content

Command Palette

Search for a command to run...

Solving Leetcode Interviews in Seconds with AI: Short Encoding of Words

Updated
3 min read

Introduction

In this blog post, we will explore how to solve the LeetCode problem "820" using AI. LeetCode is a popular platform for preparing for coding interviews, and with the help of AI tools like Chatmagic, we can generate solutions quickly and efficiently - helping you pass the interviews and get the job offer without having to study for months.

Problem Statement

A valid encoding of an array of words is any reference string s and array of indices indices such that: words.length == indices.length The reference string s ends with the '#' character. For each index indices[i], the substring of s starting from indices[i] and up to (but not including) the next '#' character is equal to words[i]. Given an array of words, return the length of the shortest reference string s possible of any valid encoding of words. Example 1: Input: words = ["time", "me", "bell"] Output: 10 Explanation: A valid encoding would be s = "time#bell#" and indices = [0, 2, 5]. words[0] = "time", the substring of s starting from indices[0] = 0 to the next '#' is underlined in "time#bell#" words[1] = "me", the substring of s starting from indices[1] = 2 to the next '#' is underlined in "time#bell#" words[2] = "bell", the substring of s starting from indices[2] = 5 to the next '#' is underlined in "time#bell#" Example 2: Input: words = ["t"] Output: 2 Explanation: A valid encoding would be s = "t#" and indices = [0]. Constraints: 1 <= words.length <= 2000 1 <= words[i].length <= 7 words[i] consists of only lowercase letters.

Explanation

Here's the breakdown of the solution:

  • Reverse and Sort: Reverse each word and sort the reversed words lexicographically. This ensures that if one word is a suffix of another, they will be adjacent after sorting.
  • Suffix Check: Iterate through the sorted reversed words, checking if the current word is a suffix of the next word. If it is, we don't need to add its length to the total.
  • Calculate Length: Add the length of each word to the total length, plus 1 for the '#' character, unless it's a suffix of the next word.

  • Runtime Complexity: O(N K log(N)), where N is the number of words and K is the maximum length of a word. Sorting dominates the runtime. Storage Complexity: O(N * K).

Code

    def minimum_length_encoding(words):
    """
    Calculates the length of the shortest reference string for encoding a list of words.

    Args:
        words: A list of strings.

    Returns:
        The length of the shortest reference string.
    """
    words = [word[::-1] for word in words]  # Reverse each word
    words.sort()  # Sort the reversed words lexicographically

    result = 0
    for i in range(len(words)):
        if i + 1 < len(words) and words[i + 1].startswith(words[i]):
            continue  # Current word is a suffix of the next word
        else:
            result += len(words[i]) + 1  # Add length of word + '#'

    return result

More from this blog

C

Chatmagic blog

2894 posts

Solving Leetcode Interviews in Seconds with AI: Short Encoding of Words