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
Don’t stop now and take your learning to the next level. Learn all the important concepts of Data Structures and Algorithms with the help of the most trusted course: DSA Self Paced. Become industry ready at a student-friendly price.
- Word Break Problem using Backtracking
- Word Break Problem | (Trie solution)
- Longest Common Prefix using Word by Word Matching
- C program to find and replace a word in a File by another given word
- C program to Replace a word in a text by another given word
- Print all ways to break a string in bracket form
- Fibonacci Word
- Tribonacci Word
- Second most repeated word in a sequence
- Possibility of a word from a given set of characters
- Next word that does not contain a palindrome and has characters from first k
- Encoding a word into Pig Latin
- Length Of Last Word in a String
- Most frequent word in an array of strings
- Print last character of each word in a string
- Print the first and last character of each word in a String
- First substring whose reverse is a word in the string
- Check if a word is present in a sentence
- Find the first repeated word in a string
- Calculate the frequency of each word in the given 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.