Problem · String
Minimum Dictionary Segments
Learn this problemProblem statement
Given a string s and an array dictionary, split all of s into a sequence of dictionary words.
Return the minimum possible number of words in a complete split. Return -1 if no complete split exists. The empty string requires zero words.
Function
minimumDictionarySegments(s: String, dictionary: String[]) → intExamples
Example 1
s = "applepie"dictionary = ["apple","app","le","pie"]return = 2The split apple | pie uses two words. No dictionary word covers the entire string.
Example 2
s = "aaaa"dictionary = ["a","aa","aaa"]return = 2Either a | aaa or aaa | a uses the minimum of two words.
Example 3
s = "catsandog"dictionary = ["cats","dog","sand","and","cat"]return = -1Every possible prefix split leaves characters that cannot be covered by a dictionary word.
Constraints
0 <= s.length <= 20000 <= dictionary.length <= 20001 <= dictionary[i].length <= 50sand every dictionary word contain lowercase English letters.- Duplicate dictionary words have no additional effect.