Given a string s, break s such that every substring of the partition can be found in the dictionary. Return the minimum break needed.
Given a dictionary ["Cat", "Mat", "Ca", "tM", "at", "C", "Dog", "og", "Do"] Input : Pattern "CatMat" Output : 1 Explanation: we can break the sentences in three ways, as follows: CatMat = [ Cat Mat ] break 1 CatMat = [ Ca tM at ] break 2 CatMat = [ C at Mat ] break 2 so the output is: 1 Input : Dogcat Output : 1
Asked in: Facebook
Solution of this problem is based on the WordBreak Trie solution and level ordered graph. We start traversing given pattern and start finding a character of pattern in a trie. If we reach a node(leaf) of a trie from where we can traverse a new word of a trie(dictionary), we increment level by one and call search function for rest of the pattern character in a trie. In the end, we return minimum Break.
MinBreak(Trie, key, level, start = 0 ) .... If start == key.length() ...update min_break for i = start to keylenght ....If we found a leaf node in trie MinBreak( Trie, key, level+1, i )
Below is the implementation of above idea
- Word Break Problem using Backtracking
- Word Break Problem | (Trie solution)
- Longest Common Prefix using Word by Word Matching
- C program to Replace a word in a text by another given word
- Print all ways to break a string in bracket form
- Tribonacci Word
- Fibonacci Word
- Possibility of a word from a given set of characters
- Length Of Last Word in a String
- Next word that does not contain a palindrome and has characters from first k
- Encoding a word into Pig Latin
- Second most repeated word in a sequence
- Count occurrences of a word in string
- Most frequent word in an array of strings
- Print last character of each word in a string
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.