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
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
- 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
- Check if permutaion of one string can break permutation of another
- Print all ways to break a string in bracket form
- Tribonacci Word
- Fibonacci Word
- Second most repeated word in a sequence
- 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
- Possibility of a word from a given set of characters
- Most frequent word in an array of strings
- Count occurrences of a word in string
- Calculate the frequency of each word in the given string
- Find the first repeated word in a string
- Capitalize the first and last character of each word in a string
- Remove the first and 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 firstname.lastname@example.org. 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.