Word Break II
Updated
Problem statement
Given a lowercase string s and a dictionary of distinct lowercase words, return every sentence formed by inserting spaces into s so that every segment is a dictionary word.
Every character of s must be used exactly once and in order. Return sentences in ascending lexicographic order. Return an empty array when no sentence is possible.
Function
wordBreakSentences(s: String, dictionary: String[]) → String[]Examples
Example 1
s = "catsanddog"dictionary = ["cat","cats","and","sand","dog"]return = ["cat sand dog","cats and dog"]Both segmentations use only dictionary words; lexicographic order places cat sand dog first.
Example 2
s = "pineapplepenapple"dictionary = ["apple","pen","applepen","pine","pineapple"]return = ["pine apple pen apple","pine applepen apple","pineapple pen apple"]There are three complete segmentations.
Example 3
s = "catsandog"dictionary = ["cats","dog","sand","and","cat"]return = []No dictionary segmentation consumes the complete string.
Constraints
1 <= s.length <= 20.1 <= dictionary.length <= 1000.- All strings contain lowercase English letters, dictionary words are distinct, and every word is nonempty.
More Google Onsite Interview questions
- Route Pattern MatcherPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- Evaluate a Nested Math ExpressionPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- Distinct Subset SumsONSITE INTERVIEW · Seen Nov 2026
- Playlist With No Same Artist in a RowONSITE INTERVIEW · Seen Nov 2026
- Delete Nodes and Return Forest RootsONSITE INTERVIEW · Seen Nov 2026
- Dice-Walk Winning ProbabilityPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- First and Last Target Position in a Mountain ArrayONSITE INTERVIEW · Seen Nov 2026
- Lowest Common Ancestor in an N-ary TreePHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- 0/1 Knapsack with Weight and Volume LimitsONSITE INTERVIEW · Seen Nov 2026
- Batched Subsequence Word MatchesONSITE INTERVIEW · Seen Nov 2026
Similar questions at other companies
- Word Break IIAmazon · PHONE SCREEN · ONSITE INTERVIEW
- Word Break IIBloomberg LP · PHONE SCREEN · ONSITE INTERVIEWBloomberg LP interview questions
- Word Break IIWex · PHONE SCREEN
- Word Break II: All SentencesMicrosoft · PHONE SCREEN · ONSITE INTERVIEWMicrosoft interview questions
- Word Search IIKotak Mahindra Bank · ONSITE INTERVIEWKotak Mahindra Bank interview questions
- Word SquaresInMobi · ONSITE INTERVIEWInMobi interview questions
- All Palindrome PartitionsExpedia · OAExpedia interview questions
- Palindrome PartitioningLondon Stock Exchange · PHONE SCREENLondon Stock Exchange interview questions
- Highlight and Rank Overlapping CitationsHarvey · PHONE SCREEN · ONSITE INTERVIEWHarvey interview questions
- Minimum Insertion Steps to Make a String PalindromeInfosys · ONSITE INTERVIEWInfosys interview questions