Redirecting to
NADOS
- Permutation - 1
- Permutations - 2
- Combinations - 1
- Combinations - 2
- Queens Permutations - 2d As 2d - Queen Chooses
- Queens Combinations - 2d As 2d - Box Chooses
- Queens Permutations - 2d As 2d - Box Chooses
- Queens Combinations - 2d As 2d - Queen Chooses
- Queens Combinations - 2d As 1d - Queen Chooses
- Nqueens Permutations - 2d As 1d - Queen Chooses
- Nqueens Combinations - 2d As 1d - Queen Chooses
- N Queens - Branch And Bound
- Nknights Combinations - 2d As 1d - Knight Chooses
- Permutations - Words - 1
- Permutations - Words - 2
- Words - K Selection - 1
- Words - K Selection - 2
- Words - K Selection - 3
- Words - K Selection - 4
- Words - K Length Words - 1
- Words - K Length Words - 2
- Words - K Length Words - 3
- Words - K Length Words - 4
- Coin Change - Combinations - 1
- Coin Change - Combinations - 2
- Coin Change - Permutations - 1
- Coin Change - Permutations - 2
- Solve Sudoku
- Crossword Puzzle
- Cryptarithmetic
- Gold Mine - 2
- Josephus Problem
- Lexicographical Numbers
- Friends Pairing - 2
- K-partitions
- K Subsets With Equal Sum
- Abbreviation Using Backtracking
- Max Score
- All Palindromic Permutations
- All Palindromic Partitions
- Pattern Matching
- Word Break - I
- Remove Invalid Parenthesis
- Tug Of War
- Largest Number Possible After At Most K Swaps
- Magnets
- Abbreviations using Backtracking
- Max Score
- N Queens Branch and Bound
- Josephus Problem
- Permutations - 1
- Lexicographical Numbers
- Gold Mine - 2
- Maximum Number After K Swaps
- K Length Words-3
- Permutations - 2
- Coin Change Combination-2
- Combinations - 1
- Friends Pairing - 2
- Remove Invalid Parentheses
- Sudoku Solver
- Word break-1
- Words - K Selection - 2
- Tug Of War
- K Subsets with Equal Sum
- Combinations - 2
- Nqueens Combinations - 2d As 1d - Queen Chooses
- Queens Combinations - 2d As 1d - Queen Chooses
- Cryptarithmetic
- Coin change combination-1
- Coin Change Permutations-2
- Coin Change Permutations-1
- K - Partitions
- All Palindromic Partitions
- Queens Combinations - 2d As 2d - Box Chooses
- Queens Permutations - 2d As 2d - Queen Chooses
- Queens Permutations - 2d As 2d - Box Chooses
- Queens Combinations - 2d As 2d - Queen Chooses
- Nknights Combinations - 2d As 1d - Knight Chooses
- Nqueens Permutations - 2d As 1d - Queen Chooses
- Permutations words-1
- Permutations Words-2
- Words - K Selection - 1
- Words-k length words-1
- Words-K Length Words-2
- Words - K Selection - 3
- Words - K Selection - 4
- Words-K Length Words-4
Word Break - I
Try First, Check Solution later
1. You should first read the question and watch the question video.2. Think of a solution approach, then try and submit the question on editor tab.3. We strongly advise you to watch the solution video for prescribed approach.1. You are given n space separated strings, which represents a dictionary of words.Input Format
2. You are given another string which represents a sentence.
3. You have to print all possible sentences from the string, such that words of the sentence are
present in dictionary.
Note -> Check out the question video and write the recursive code as it is intended without
changing signature. The judge can't force you but intends you to teach a concept.
A number nOutput Format
n strings representing words
a string representing a sentence
Check the sample ouput and question video.Question Video Constraints
1 <= number of words <= 10Sample Input
1 <= length of each word <= 15
1 <= length of sentence <= 1000
11Sample Output
i like pep coding pepper eating mango man go in pepcoding
ilikepeppereatingmangoinpepcoding
i like pepper eating man go in pep coding
i like pepper eating man go in pepcoding
i like pepper eating mango in pep coding
i like pepper eating mango in pepcoding
-
Editorial
We will solve this question with the recursion. We will first store all the given words in a set. Then we will generate all the prefixes of the current word. If the prefix is present in the set, we will call the recursive function for the remaining string. When the length of the string is zero, we will print the ans and return.
Signature -
public static void wordBreak(String str, String ans, HashSet < String > dict)
str = Input string
ans = answer till the current level
dict = Set containing all the given words
Code -
public static void wordBreak(String str, String ans, HashSet < String > dict) { if (str.length() == 0) { System.out.println(ans); return; } for (int i = 0; i < str.length(); i++) { String left = str.substring(0, i + 1); if (dict.contains(left)) { String right = str.substring(i + 1); wordBreak(right, ans + left + " ", dict); } } }
java false
Code Explained -
With the help of for loop, we generate all the prefixes. If the prefix is present in the dict, we first find the suffix left and call the recursive function for the remaining string. We also append the prefix to the ans.
Base Case -
When the length of the str is zero, it means we have used the whole string. We will print the ans and return.
-
Asked in Companies
-
Related Topics
Your profile is incomplete!
Please click here to complete your profile to continue submitting questions.
Video Solution
Code Solution
Editor Settings
Font Size
Key Binding
Keyboard Shortcut
- fold Alt-L|Ctrl-F1
- unfold Alt-Shift-L|Ctrl-Shift-F1
- Gotoend Ctrl-End
- Gotostart Ctrl-Home
- Movelinesup Alt-Up
- Movelinesdown Alt-Down
- Undo Ctrl-Z
- Redo Ctrl-Shift-Z|Ctrl-Y
- Replace Ctrl-H
- Togglecomment Ctrl-/
- ToggleBlockComment Ctrl-Shift-/
- Removeline Ctrl-D
Editor Settings
Font Size
Key Binding
Keyboard Shortcut
- fold Alt-L|Ctrl-F1
- unfold Alt-Shift-L|Ctrl-Shift-F1
- Gotoend Ctrl-End
- Gotostart Ctrl-Home
- Movelinesup Alt-Up
- Movelinesdown Alt-Down
- Undo Ctrl-Z
- Redo Ctrl-Shift-Z|Ctrl-Y
- Replace Ctrl-H
- Togglecomment Ctrl-/
- ToggleBlockComment Ctrl-Shift-/
- Removeline Ctrl-D