Generalized Abbreviation

IF
AlgoAxiomStaff Engineers
JSTS
Medium20 mins

Given a word, generate all possible generalized abbreviations of the word.

A generalized abbreviation of a word can be constructed by replacing any number of non-overlapping and non-adjacent substrings with their respective lengths. For example, "abcde" can be abbreviated as "a4", "a1c1e", "5", "2c2", etc.

Two adjacent numbers are not allowed — they must be merged into a single number. For example, "1b1" is valid but "11" is not (it should be "2" instead).

Examples

Example 1:

Input: word = "word"

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

Explanation: Each character is either kept or replaced. Consecutive replaced characters are merged into a single count.

Example 2:

Input: word = "a"

Output: ["1","a"]

Explanation: The single character can be kept as "a" or abbreviated as "1".

Constraints

  • 1 <= word.length <= 15
  • word consists of only lowercase English letters
Source: Subsets pattern — AlgoAxiom
JavaScript
Test Case 1
root = [1, 2, 3]
Test Case 2
root = [1, 2, 3, 4, 5]
Idle