Word Break II
Updated
Problem statement
Given a non-empty lowercase string s and an array wordDict of distinct lowercase words, insert spaces into s to form valid sentences.
Each segment of a sentence must appear in wordDict. A dictionary word may be used more than once, and every character of s must belong to exactly one segment.
Return every distinct valid sentence in lexicographically increasing order. If no valid sentence can be formed, return an empty list.
Function
wordBreak(s: String, wordDict: List<String>) → List<String>Examples
Example 1
s = "catsanddog"wordDict = ["cat","cats","and","sand","dog"]return = ["cat sand dog","cats and dog"]The string can be segmented as cat sand dog or cats and dog. Both sentences use only dictionary words and appear in lexicographic order.
Example 2
s = "pineapplepenapple"wordDict = ["apple","pen","applepen","pine","pineapple"]return = ["pine apple pen apple","pine applepen apple","pineapple pen apple"]There are three complete segmentations. The word apple is reused where needed, and the resulting sentences are returned in lexicographic order.
Example 3
s = "catsandog"wordDict = ["cats","dog","sand","and","cat"]return = []No sequence of dictionary words covers every character of s, so the result is empty.
Constraints
1 <= s.length <= 201 <= wordDict.length <= 10001 <= wordDict[i].length <= 10sand every word inwordDictcontain only lowercase English letters.- All words in
wordDictare distinct. - The total length of all valid output sentences does not exceed
10^5.
More InMobi Phone Screen questions
- House Robber IPHONE SCREEN · Seen Aug 2026
- Bus Station Seat AllotmentPHONE SCREEN · Seen Mar 2026
- Find Missing and Duplicate IntegerPHONE SCREEN · Seen Mar 2026
- Frequency Equals Card ValuePHONE SCREEN · Seen Mar 2026
- Frog Position After T SecondsPHONE SCREEN · Seen Mar 2026
- IPOPHONE SCREEN · Seen Mar 2026
- Evaluate DivisionPHONE SCREEN · Seen Dec 2025
- Compare Version NumbersPHONE SCREEN · Seen Feb 2022
- Count Array DerangementsPHONE SCREEN · Seen Feb 2022
- Maximum Frequency StackPHONE SCREEN · Seen Feb 2022
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 IIBloomberg LP · PHONE SCREEN · ONSITE INTERVIEWBloomberg LP interview questions
- Word Break II: All SentencesMicrosoft · PHONE SCREEN · ONSITE INTERVIEWMicrosoft interview questions
- Palindrome PartitioningLondon Stock Exchange · PHONE SCREENLondon Stock Exchange interview questions
- All Palindrome PartitionsExpedia · OAExpedia interview questions
- Maximum Currency Conversion Along a Simple PathRippling · PHONE SCREEN · ONSITE INTERVIEWRippling interview questions
- Expression Add OperatorsPinterest · PHONE SCREENPinterest interview questions
- Palindrome with K DeletionsWhatNot · PHONE SCREENWhatNot interview questions