Generate all generalized abbreviations of a word

Quick Overview

Given a lowercase word of up to 15 letters, return every generalized abbreviation in sorted order, where chosen substrings are replaced by their lengths and no two replaced substrings overlap or touch. It tests exhaustive enumeration with bitmasks or backtracking and careful handling of runs of replaced characters.

Generate all generalized abbreviations of a word

Company: ByteDance

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: medium

Interview Round: Other

A generalized abbreviation of a word is formed by choosing any number of non-overlapping, non-adjacent substrings and replacing each one with its length. For example, `"abcde"` can be abbreviated as: - `"a3e"` (`"bcd"` becomes `"3"`) - `"1bcd1"` (`"a"` and `"e"` each become `"1"`) - `"5"` (`"abcde"` becomes `"5"`) - `"abcde"` (nothing is replaced) These are not valid: - `"23"` replaces `"ab"` with `"2"` and `"cde"` with `"3"`, but the two substrings are adjacent. - `"22de"` replaces `"ab"` with `"2"` and `"bc"` with `"2"`, but the two substrings overlap. Given a string `word`, return every generalized abbreviation of `word`. ### Function Signature ```python def generate_abbreviations(word: str) -> list[str]: ``` ### Rules - Each replaced substring is written as its length in decimal. Because replaced substrings cannot be adjacent, two numbers never appear side by side in a valid abbreviation. - Each abbreviation appears exactly once in the result. - Return the list sorted in ascending lexicographic order by character code, which is the order Python's `sorted()` gives. Digits sort before lowercase letters, so `"4"` comes before `"w3"`. ### Constraints - `1 <= len(word) <= 15` - `word` consists of lowercase English letters only. ### Examples ```text Input: word = "word" Output: ["1o1d", "1o2", "1or1", "1ord", "2r1", "2rd", "3d", "4", "w1r1", "w1rd", "w2d", "w3", "wo1d", "wo2", "wor1", "word"] ``` Each of the 4 characters is either kept or replaced, and each of the 16 keep-or-replace patterns gives a different abbreviation. In sorted order every string that starts with a digit comes before the ones that start with `w`. ```text Input: word = "a" Output: ["1", "a"] ``` The single character is either kept (`"a"`) or replaced by its length (`"1"`), and `"1"` sorts before `"a"`.

Overview: Given a lowercase word of up to 15 letters, return every generalized abbreviation in sorted order, where chosen substrings are replaced by their lengths and no two replaced substrings overlap or touch. It tests exhaustive enumeration with bitmasks or backtracking and careful handling of runs of replaced characters.

ByteDance logo
ByteDance
Nov 24, 2026
mediumSoftware EngineerOtherCoding & Algorithms
0
0

A generalized abbreviation of a word is formed by choosing any number of non-overlapping, non-adjacent substrings and replacing each one with its length.

For example, "abcde" can be abbreviated as:

  • "a3e" ( "bcd" becomes "3" )
  • "1bcd1" ( "a" and "e" each become "1" )
  • "5" ( "abcde" becomes "5" )
  • "abcde" (nothing is replaced)

These are not valid:

  • "23" replaces "ab" with "2" and "cde" with "3" , but the two substrings are adjacent.
  • "22de" replaces "ab" with "2" and "bc" with "2" , but the two substrings overlap.

Given a string word, return every generalized abbreviation of word.

Function Signature

def generate_abbreviations(word: str) -> list[str]:

Rules

  • Each replaced substring is written as its length in decimal. Because replaced substrings cannot be adjacent, two numbers never appear side by side in a valid abbreviation.
  • Each abbreviation appears exactly once in the result.
  • Return the list sorted in ascending lexicographic order by character code, which is the order Python's sorted() gives. Digits sort before lowercase letters, so "4" comes before "w3" .

Constraints

  • 1 <= len(word) <= 15
  • word consists of lowercase English letters only.

Examples

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

Each of the 4 characters is either kept or replaced, and each of the 16 keep-or-replace patterns gives a different abbreviation. In sorted order every string that starts with a digit comes before the ones that start with w.

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

The single character is either kept ("a") or replaced by its length ("1"), and "1" sorts before "a".