Word Break II: All Sentences
Updated
Problem statement
Given a nonempty string s and an array of distinct words wordDict, return every way to split the entire string into dictionary words.
Represent each split as a sentence with exactly one space between adjacent words. Do not add a leading or trailing space. A dictionary word may be used more than once.
Return the distinct sentences in lexicographically increasing order. If no complete split exists, return an empty array.
Function
wordBreakSentences(s: String, wordDict: String[]) → String[]Examples
Example 1
s = "redblue"wordDict = ["red","blue","redblue"]return = ["red blue","redblue"]Either use the two words red and blue, or use the single word redblue. The sentence containing a space sorts first.
Example 2
s = "redgreen"wordDict = ["red","blue"]return = []The prefix red is valid, but the remaining green cannot be covered by dictionary words.
Example 3
s = "aa"wordDict = ["a","aa"]return = ["a a","aa"]The word a may be reused, so both a a and aa are valid complete sentences.
Constraints
1 <= s.length <= 20.1 <= wordDict.length <= 1000.1 <= wordDict[i].length <= 10.sand every dictionary word contain only lowercase English letters.- The dictionary words are distinct.
- The sum of the lengths of all returned sentences, counting spaces, is at most
10^5.
More Microsoft Phone Screen questions
- Config Store with Priority OverridesPHONE SCREEN · Seen Nov 2026
- Corporation Reporting LinesPHONE SCREEN · Seen Oct 2026
- Random Forest Classifier from ScratchPHONE SCREEN · Seen Oct 2026
- Least Recently Used CacheOA · PHONE SCREEN · ONSITE INTERVIEW · Seen Oct 2026
- Greedy and Beam Sentence DecodingPHONE SCREEN · ONSITE INTERVIEW · Seen Sep 2026
- Top K Frequent ElementsPHONE SCREEN · Seen Sep 2026
- 3SumPHONE SCREEN · ONSITE INTERVIEW · Seen Sep 2026
- Arithmetic Expression EvaluatorPHONE SCREEN · Seen Sep 2026
- Employee Hierarchy Cycle and Depth AnalysisPHONE SCREEN · Seen Sep 2026
- Most Visited String SectorsPHONE SCREEN · Seen Sep 2026
Similar questions at other companies
- Balanced Parentheses with Sequential Left DeletionsGoogle · PHONE SCREENGoogle interview questions
- Word Break IIAmazon · PHONE SCREEN · ONSITE INTERVIEW
- Word Break IIWex · PHONE SCREEN
- Word Break IIInMobi · PHONE SCREENInMobi interview questions
- Word Break IIBloomberg LP · PHONE SCREEN · ONSITE INTERVIEWBloomberg LP interview questions
- Palindrome PartitioningLondon Stock Exchange · PHONE SCREENLondon Stock Exchange interview questions
- All Palindrome PartitionsExpedia · OAExpedia interview questions
- Minimum Insertion Steps to Make a String PalindromeInfosys · ONSITE INTERVIEWInfosys interview questions
- Wildcard MatchingTripjack · ONSITE INTERVIEWTripjack interview questions
- All Balanced Strings from Minimum Parenthesis InsertionsRubrik · ONSITE INTERVIEWRubrik interview questions