Skip to content

sarthakandhare007/Word-Break-II-Problem-in-Java

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

3 Commits
 
 
 
 

Repository files navigation

Problem Statement

Given a string s and a dictionary of words wordDict, add spaces in s to construct all possible sentences such that every word is a valid word in wordDict. Return all such possible sentences in any order.


🔹 Example 1:

Input:

s = "catsanddog" wordDict = ["cat", "cats", "and", "sand", "dog"]

Output:

["cats and dog", "cat sand dog"]


🔹 Example 2:

Input:

s = "pineapplepenapple" wordDict = ["apple","pen","applepen","pine","pineapple"]

Output:

["pine apple pen apple", "pineapple pen apple", "pine applepen apple"]


⚙️ Approach (Explanation)

This is a backtracking + memoization (DP) problem.

  1. We recursively try to break the string at every possible point.

  2. If a substring exists in the dictionary, we recursively solve for the remaining part.

  3. To avoid re-computation, we store results of subproblems in a HashMap (memoization).

  4. Finally, we join all combinations that form valid sentences.

Word-Break-II-Problem-in-Java

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages