LeetCode 0320. Generalized Abbreviation Solution in Java, Python, C++, JavaScript, Go & Rust | Explanation + Code

CoderIndeed
0
0320. Generalized Abbreviation

Description

A word's generalized abbreviation can be constructed by taking any number of non-overlapping and non-adjacent substrings and replacing them with their respective lengths.

  • For example, "abcde" can be abbreviated into:
    <ul>
    	<li><code>&quot;a3e&quot;</code> (<code>&quot;bcd&quot;</code> turned into <code>&quot;3&quot;</code>)</li>
    	<li><code>&quot;1bcd1&quot;</code> (<code>&quot;a&quot;</code> and <code>&quot;e&quot;</code> both turned into <code>&quot;1&quot;</code>)</li>
    	<li><code>&quot;5&quot;</code> (<code>&quot;abcde&quot;</code> turned into <code>&quot;5&quot;</code>)</li>
    	<li><code>&quot;abcde&quot;</code> (no substrings replaced)</li>
    </ul>
    </li>
    <li>However, these abbreviations are <strong>invalid</strong>:
    <ul>
    	<li><code>&quot;23&quot;</code> (<code>&quot;ab&quot;</code> turned into <code>&quot;2&quot;</code> and <code>&quot;cde&quot;</code> turned into <code>&quot;3&quot;</code>) is invalid as the substrings chosen are adjacent.</li>
    	<li><code>&quot;22de&quot;</code> (<code>&quot;ab&quot;</code> turned into <code>&quot;2&quot;</code> and <code>&quot;bc&quot;</code> turned into <code>&quot;2&quot;</code>) is invalid as the substring chosen overlap.</li>
    </ul>
    </li>
    

Given a string word, return a list of all the possible generalized abbreviations of word. Return the answer in any order.

 

Example 1:

Input: word = "word"
Output: ["4","3d","2r1","2rd","1o2","1o1d","1or1","1ord","w3","w2d","w1r1","w1rd","wo2","wo1d","wor1","word"]

Example 2:

Input: word = "a"
Output: ["1","a"]

 

Constraints:

  • 1 <= word.length <= 15
  • word consists of only lowercase English letters.

Solutions

Solution 1: DFS

We design a function dfs(i), which returns all possible abbreviations for the string word[i:].

The execution logic of the function dfs(i) is as follows:

If i ≥ n, it means that the string word has been processed, and we directly return a list composed of an empty string.

Otherwise, we can choose to keep word[i], and then add word[i] to the front of each string in the list returned by dfs(i + 1), and add the obtained result to the answer.

We can also choose to delete word[i] and some characters after it. Suppose we delete word[i..j), then the j th character is not deleted, and then add j - i to the front of each string in the list returned by dfs(j + 1), and add the obtained result to the answer.

Finally, we call dfs(0) in the main function.

The time complexity is O(n × 2^n), and the space complexity is O(n). Where n is the length of the string word.

PythonJavaC++GoTypeScript
class Solution: def generateAbbreviations(self, word: str) -> List[str]: def dfs(i: int) -> List[str]: if i >= n: return [""] ans = [word[i] + s for s in dfs(i + 1)] for j in range(i + 1, n + 1): for s in dfs(j + 1): ans.append(str(j - i) + (word[j] if j < n else "") + s) return ans n = len(word) return dfs(0)(code-box)

Solution 2: Binary Enumeration

Since the length of the string word does not exceed 15, we can use the method of binary enumeration to enumerate all abbreviations. We use a binary number i of length n to represent an abbreviation, where 0 represents keeping the corresponding character, and 1 represents deleting the corresponding character. We enumerate all i in the range of [0, 2^n), convert it into the corresponding abbreviation, and add it to the answer list.

The time complexity is O(n × 2^n), and the space complexity is O(n). Where n is the length of the string word.

PythonJavaC++Go
class Solution: def generateAbbreviations(self, word: str) -> List[str]: n = len(word) ans = [] for i in range(1 << n): cnt = 0 s = [] for j in range(n): if i >> j & 1: cnt += 1 else: if cnt: s.append(str(cnt)) cnt = 0 s.append(word[j]) if cnt: s.append(str(cnt)) ans.append("".join(s)) return ans(code-box)

Post a Comment

0Comments

Post a Comment (0)

#buttons=(Accept !) #days=(20)

Our website uses cookies to enhance your experience. Check Now
Accept !