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 OverviewInput: "abc" → Generate All Valid Abbreviationsabcpos 0pos 1pos 2KeepEitherAbbreviateAll Valid Combinations (8 total)"abc""ab1""a1c""a2""1bc""1b1""2c""3"Rule: Can replace non-adjacent, non-overlapping substringsEach number represents the count of abbreviated characters
Understanding the Visualization
1
Input
Original word with each character position
2
Process
Choose to keep or abbreviate each character (non-adjacent)
3
Output
All possible valid abbreviations
Key Takeaway
🎯 Key Insight: Each character position has exactly 2 choices: keep the character or include it in an abbreviation group
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