Quick Overview

Enumerate all complete syllable segmentations of an unseparated lowercase string. Preserve the empty-input convention, deduplicate dictionary entries, order results canonically, and explain the output-sensitive cost of memoized suffix search.

Return every complete segmentation into valid syllables

Company: Microsoft

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: hard

Interview Round: Technical Screen

Given a lowercase string with no separators and a collection of valid syllables, return all complete segmentations of the string into those syllables. A syllable may be reused. Every character must belong to exactly one chosen syllable. ### Function Signature ```python def all_syllable_segmentations(text: str, syllables: list[str]) -> list[list[str]]: ``` ### Rules - Return each segmentation as its ordered list of syllables. - Return distinct segmentations in lexicographic order, comparing syllable strings and then list positions. - Duplicate dictionary entries do not create duplicate results. - Return `[]` if the input text is empty or no complete segmentation exists. ### Constraints These bounds define this practice version. - `0 <= len(text) <= 30` and text contains only lowercase ASCII letters. - There are between 0 and 1000 dictionary entries, each containing 1 through 10 lowercase ASCII letters. - The total number of syllable occurrences across the returned segmentations is at most 100000. ### Examples ```text Input: text = "fangan", syllables = ["fan", "gan", "fang", "an"] Output: [["fan", "gan"], ["fang", "an"]] ``` Both outputs consume the full string. ```text Input: text = "abc", syllables = ["a", "ab"] Output: [] ``` No permitted syllable consumes the remaining suffix. ```text Input: text = "", syllables = ["a"] Output: [] ``` The external empty-input convention differs from the successful internal empty suffix used during recursion.

Overview: Enumerate all complete syllable segmentations of an unseparated lowercase string. Preserve the empty-input convention, deduplicate dictionary entries, order results canonically, and explain the output-sensitive cost of memoized suffix search.

Read the full Microsoft Software Engineer interview experience this question came from

Given a lowercase string with no separators and a collection of valid syllables, return all complete segmentations of the string into those syllables. A syllable may be reused. Every character must belong to exactly one chosen syllable. ### Function Signature ```python def all_syllable_segmentations(text: str, syllables: list[str]) -> list[list[str]]: ``` ### Rules - Return each segmentation as its ordered list of syllables. - Return distinct segmentations in lexicographic order, comparing syllable strings and then list positions. - Duplicate dictionary entries do not create duplicate results. - Return `[]` if the input text is empty or no complete segmentation exists. ### Constraints These bounds define this practice version. - `0 <= len(text) <= 30` and text contains only lowercase ASCII letters. - There are between 0 and 1000 dictionary entries, each containing 1 through 10 lowercase ASCII letters. - The total number of syllable occurrences across the returned segmentations is at most 100000. ### Examples ```text Input: text = "fangan", syllables = ["fan", "gan", "fang", "an"] Output: [["fan", "gan"], ["fang", "an"]] ``` Both outputs consume the full string. ```text Input: text = "abc", syllables = ["a", "ab"] Output: [] ``` No permitted syllable consumes the remaining suffix. ```text Input: text = "", syllables = ["a"] Output: [] ``` The external empty-input convention differs from the successful internal empty suffix used during recursion.

Constraints

  • `0 <= len(text) <= 30` and text contains only lowercase ASCII letters.
  • There are between 0 and 1000 dictionary entries, each containing 1 through 10 lowercase ASCII letters.
  • The total number of syllable occurrences across the returned segmentations is at most 100000.

Examples

Input: ('fangan', ['fan', 'gan', 'fang', 'an'])

Expected Output: [['fan', 'gan'], ['fang', 'an']]

Explanation: Both paths consume the full input and the shorter prefix comes first.

Input: ('abc', ['a', 'ab'])

Expected Output: []

Explanation: Every matching prefix leaves an impossible suffix.

Hints

  1. A prefix match can be valid locally while leaving an impossible suffix.
  2. Matching syllables that start at the same position can be compared by their lengths.

Return every complete segmentation into valid syllables

hardMicrosoftSoftware Engineer

Given a lowercase string with no separators and a collection of valid syllables, return all complete segmentations of the string into those syllables. A syllable may be reused. Every character must belong to exactly one chosen syllable.

Function Signature

def all_syllable_segmentations(text: str, syllables: list[str]) -> list[list[str]]:

Rules

  • Return each segmentation as its ordered list of syllables.
  • Return distinct segmentations in lexicographic order, comparing syllable strings and then list positions.
  • Duplicate dictionary entries do not create duplicate results.
  • Return [] if the input text is empty or no complete segmentation exists.

Constraints

These bounds define this practice version.

  • 0 <= len(text) <= 30 and text contains only lowercase ASCII letters.
  • There are between 0 and 1000 dictionary entries, each containing 1 through 10 lowercase ASCII letters.
  • The total number of syllable occurrences across the returned segmentations is at most 100000.

Examples

Input: text = "fangan", syllables = ["fan", "gan", "fang", "an"]
Output: [["fan", "gan"], ["fang", "an"]]

Both outputs consume the full string.

Input: text = "abc", syllables = ["a", "ab"]
Output: []

No permitted syllable consumes the remaining suffix.

Input: text = "", syllables = ["a"]
Output: []

The external empty-input convention differs from the successful internal empty suffix used during recursion.

Examples

Example 1
Input
('fangan', ['fan', 'gan', 'fang', 'an'])
Output
[['fan', 'gan'], ['fang', 'an']]
Notes
Both paths consume the full input and the shorter prefix comes first.
Example 2
Input
('abc', ['a', 'ab'])
Output
[]
Notes
Every matching prefix leaves an impossible suffix.

Constraints

  • •`0 <= len(text) <= 30` and text contains only lowercase ASCII letters.
  • •There are between 0 and 1000 dictionary entries, each containing 1 through 10 lowercase ASCII letters.
  • •The total number of syllable occurrences across the returned segmentations is at most 100000.

Comments (0)

No comments yet. Be the first to comment!

Submit your answer

Approved first answer today earns 30 XP
Bold⌘BItalic⌘IInline code⌘`Bullet listTab to nest (○ ■)Numbered listTab to nest (a. i.)QuoteCode block
Formatting help
Signature
def all_syllable_segmentations(text: str, syllables: list[str]) -> list[list[str]]:
Sample call
all_syllable_segmentations('fangan', ['fan', 'gan', 'fang', 'an'])
Language
Case 1
Input
('fangan', ['fan', 'gan', 'fang', 'an'])
Expected
[['fan', 'gan'], ['fang', 'an']]
Case 2
Input
('abc', ['a', 'ab'])
Expected
[]

Browse More Questions

Show the approach

Approach

Use a set for the dictionary. Work backward through the text, recording ends of dictionary matches that leave a segmentable suffix. The empty suffix at the end is internally segmentable. Therefore a position is segmentable exactly when it has at least one recorded end. Check candidate lengths from one to ten in increasing order.

For nonempty text, do a depth-first enumeration from the beginning, following only recorded ends. Record the path when it reaches the final position. The edges represent distinct syllables and every complete segmentation follows uniquely through them. Any two matches at the same start share a prefix, and the shorter one sorts first, so visiting edges in increasing end order yields the required lexicographic order without a final sort.

Time complexity:
O(D + n × L² + R), for D dictionary characters, text length n, syllable limit L=10, and total output syllable occurrences R
Space complexity:
O(D + n × L) auxiliary space, plus the output