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
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
💡
Explanation
AI Ready
💡 Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code