Word Break-1

You are given a non-empty string containing no spaces (say sentence) and a dictionary of list of non-empty strings (say the list of words). You are supposed to construct and return all possible sentences after adding spaces in the originally given string (sentence), where each word must exist in the given dictionary.

Note :
The same word from a dictionary can be used as many times as possible to make sentences.
Input format :
The first line contains an integer value ‘N’ which denotes the size of the dictionary.
Next ‘N’ line contains a non-empty string “denoting words”.
Next ‘(N+1)’ th line contains a non-empty string without any space.
Output format :
Print each possible sentence after adding spaces in different lines.
Note :
You do not need to print anything, it has already been taken care of. Order of sentences does not matter.
Constraints :
1 <= N <= 100
1 <= M <= 16
1 <= W <= 16
Where ‘N’ is the length of a dictionary , ‘W’ is the length of the “word” in a dictionary and ‘M’ is the length of a sentence.
Time limit: 1 sec.
CodingNinjas
author
2y
Brute Force Approach

We need to keep exploring the given string from current position ‘i’ to until we wouldn’t find a position such that substring ‘i’ to ‘j’ exists in the dictionary.

The algorithm look...read more

CodingNinjas
author
2y
Dynamic Programming Approach

While exploring all possibilities, we are also going to store whatever solution we got till now for a sentence so that we will be able to avoid redundant function calls.

Alg...read more

CodingNinjas
author
2y
Trie based Approach

The idea is to use Trie data structure. 

A trie is a tree-like data structure whose nodes store the letters of an alphabet. By structuring the nodes in a particular way, words and strings can be retrieved from the structure by traversing down a branch path of the tree.

Using trie will help us to save time and space.
 

A sample trie formed by adding words ‘ate’, ‘ant’, ‘ants’, ‘bat’, ‘ball’, ‘and’ is shown below. The nodes with green color represent the end of the word. 

To know more about tries, click here
 

Algorithm: 

  • Generate a trie by adding the words of the dictionary. The last letter of each word should be marked as the terminal or end of the word.
  • After trie is built, string ‘S’ can be easily compared letter by letter in the trie.
  • Traverse the string ‘S’ from left to right:
    • If the current character is not present as the children of the root of the trie, return from here as we can’t break the given string in valid words.
    • If at any point, we encounter a leaf in the trie, we can assume that we have found a valid word in the dictionary.
      • If we reach the end of the string ‘S’, we will add the sentence formed by breaking the string ‘S’ to our output array.
      • Else, recursively try to break the remaining string.
  • Return the output array containing all the possible sentences obtained from breaking the given string ‘S’.
Space Complexity: O(1)Explanation:

O(26 * K * | word |), where K is the number of strings in the dictionary, and | word | is the length of a word in the dictionary.

In the worst case, for every word in the dictionary, we will create | word | nodes and every node will have an array of size 26 (to store the children of the node). Since there are K strings in the dictionary, the number of nodes will be O(K * | word |) and thus the total space complexity will be O(26 * K * | word |).

Recursion stack space of O(N) is also used. Thus, the final space complexity is O((26 * K * | word |) + N) = O(26 * K * | word |).

Time Complexity: O(1)Explanation:

O(N * (2 ^ (N - 1)), where ‘N’ is the length of the sentence.

Time complexity would be, in the worst case, O(N * (2 ^ (N - 1))) when each substring of the sentence exists as a word in the dictionary like sentence = “xxx” and dictionary={“x”, “xx”, “xxx”}. We need to explore the possible combinations. While our map is avoiding many redundant function calls, but still this approach will also go in O(N * (2 ^ (N - 1))) in the worst case. Though the complexity of this approach is the same as the previous approach, yet this approach is better because the time taken to search for a word in the trie is much less than that in a hashset.

Add answer anonymously...
Groww Software Developer Intern Interview Questions
Stay ahead in your career. Get AmbitionBox app
qr-code
Helping over 1 Crore job seekers every month in choosing their right fit company
65 L+

Reviews

4 L+

Interviews

4 Cr+

Salaries

1 Cr+

Users/Month

Contribute to help millions
Get AmbitionBox app

Made with ❤️ in India. Trademarks belong to their respective owners. All rights reserved © 2024 Info Edge (India) Ltd.

Follow us
  • Youtube
  • Instagram
  • LinkedIn
  • Facebook
  • Twitter