Generalized Abbreviation - Problem

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:

  • "a3e" ("bcd" turned into "3")
  • "1bcd1" ("a" and "e" both turned into "1")
  • "5" ("abcde" turned into "5")
  • "abcde" (no substrings replaced)

However, these abbreviations are invalid:

  • "23" ("ab" turned into "2" and "cde" turned into "3") is invalid as the substrings chosen are adjacent.
  • "22de" ("ab" turned into "2" and "bc" turned into "2") is invalid as the substring chosen overlap.

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

Input & Output

Example 1 — Basic Case
$ Input: word = "word"
Output: ["4","3d","2r1","2rd","1o1d","1o2","1od1","1ord","w2d","w3","w1r1","w1rd","wo1d","wo2","wor1","word"]
💡 Note: All possible ways to abbreviate "word" by replacing non-adjacent substrings with their lengths
Example 2 — Short Word
$ Input: word = "a"
Output: ["1","a"]
💡 Note: Single character can either be abbreviated as "1" or kept as "a"
Example 3 — Two Characters
$ Input: word = "hi"
Output: ["2","1i","h1","hi"]
💡 Note: Two characters give us 4 combinations: abbreviate both, abbreviate first, abbreviate second, or keep both

Constraints

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

Visualization

Tap to expand
Generalized Abbreviation INPUT word = "word" w idx 0 o idx 1 r idx 2 d idx 3 Each char has 2 choices: Keep letter Abbreviate 2^4 = 16 combinations Example patterns: "word" = keep all "4" = abbr all "w2d" = mixed Length: 4 characters ALGORITHM STEPS 1 Backtrack Setup Track position, count, result 2 Choice 1: Abbreviate Increment count, recurse 3 Choice 2: Keep Letter Flush count, add char 4 Base Case At end, add to result list Recursion Tree (partial) w 1 abbr w keep 2 1o ...continues for all paths FINAL RESULT 16 unique abbreviations: "4" "3d" "2r1" "2rd" "1o1d" "1o2" "1or1" "1ord" "w2d" "w3" "w1r1" "w1rd" "wo1d" "wo2" "wor1" "word" OK - 16 abbreviations found All unique, no adjacent nums Complexity Analysis Time: O(n * 2^n) Space: O(n) recursion depth Pattern Validation "12" invalid - "3" is correct Key Insight: Use backtracking with two choices at each position: either abbreviate (increment counter) or keep the letter (flush counter if non-zero, then append character). The counter tracks consecutive abbreviated characters. This ensures numbers are never adjacent - we only output a number when we encounter a kept letter or reach the end. TutorialsPoint - Generalized Abbreviation | Optimal Backtracking Approach
Asked in
Google 15 Facebook 12 Amazon 8
33.0K Views
Medium Frequency
~25 min Avg. Time
847 Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen