Update appNew update is available. Click here to update.
sidenav-btnClose
Topic list
Word Break-1
MEDIUM
36 mins
55 upvotes
Strings
Dynamic Programming
Topics (Covered in this problem)
Problem solved
Badge
Skill meter
Strings
-
-
Dynamic Programming
-
-
Other topics
Problem solved
Badge
Skill meter
Matrices (2D Arrays)
-
-
Linked List
-
-
Sorting
-
-
Binary Search
-
-
Stacks & Queues
-
-
Trees
-
-
Graph
-
-
Greedy
-
-
Tries
-
-
Arrays
-
-
SQL
-
-
Binary Search Trees
-
-
Heap
-
-
Bit Manipulation
-
-
Solve problems & track your progress
Checkout your overall progress in every topic here
Become
userLevel
Sensei
in DSA topics
Open the topic and solve more problems associated with it to improve your skills
Check out the skill meter for every topic
See how many problems you are left with to solve for cracking any stage. Score more than zero to get your progress counted.

Word Break-1

Contributed by
Anup Kumar Singh
Medium
yellow-spark
0/80
Avg time to solve 36 mins
Success Rate 55 %
Share
55 upvotes

Problem Statement

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.
Detailed explanation ( Input/output format, Notes, Constraints, Images )
Sample Input 1:
6
god
is
now
no
where
here
godisnowherenowhere
Sample Output 1:
god is no where no where
god is no where now here
god is now here no where
god is now here now here
Explanation to Sample Input 1:
One way to make sentences is to take “god” and append a space, then take “is”  and append space, take “now” from the dictionary and take “here” as well. 
Similarly, for other sentences, also we can add space to get other possible sentences.    Note that you can reuse dictionary words as “no” and “now” are used two times to make the same sentence.
Sample Input 2:
4
god
is
no
here
godisnowhere
Sample Output 2:
Explanation to Sample Input 2::
We can not make any sentence because after making “god is no” we will be stuck with “where”. There is no way to break “where” further “such that we can get any word from the dictionary”.
Reset Code
Full screen
Auto
copy-code
Console