Skip to content
Programmingoneonone
Programmingoneonone
  • CS Subjects
    • Internet of Things (IoT)
    • Digital Communication
    • Human Values
    • Cybersecurity
  • Programming Tutorials
    • C Programming
    • Data structures and Algorithms
    • 100+ Java Programs
    • 100+ C Programs
  • HackerRank Solutions
    • HackerRank Algorithms Solutions
    • HackerRank C problems solutions
    • HackerRank C++ problems solutions
    • HackerRank Java problems solutions
    • HackerRank Python problems solutions
Programmingoneonone
Programmingoneonone

Leetcode Word Break II problem solution

YASH PAL, 31 July 202419 January 2026

In this Leetcode Word Break II problem solution, we have given a string s and a dictionary of strings wordDict, add spaces in s to construct a sentence where each word is a valid dictionary word. Return all such possible sentences in any order.

Leetcode Word Break II problem solution

Leetcode Word Break II problem solution in Python.

class Solution:
    def wordBreak(self, s: str, wordDict: List[str]) -> List[str]:
        res = []
        self.wordDict = set(wordDict)
        self.dfs(s, [], res)
        return res
    def dfs(self, s, path, res):
        if not s:
            res.append(" ".join(path))
            return
        
        for i in range(len(s)):
            if s[:i+1] in self.wordDict:
                self.dfs(s[i+1:], path+[s[:i+1]], res)

Word Break II problem solution in Java.

public List<String> wordBreak(String s, Set<String> wordDict) {
    return DFS(s, wordDict, new HashMap<String, LinkedList<String>>());
}       

List<String> DFS(String s, Set<String> wordDict, HashMap<String, LinkedList<String>>map) {
    if (map.containsKey(s)) 
        return map.get(s);
        
    LinkedList<String>res = new LinkedList<String>();     
    if (s.length() == 0) {
        res.add("");
        return res;
    }               
    for (String word : wordDict) {
        if (s.startsWith(word)) {
            List<String>sublist = DFS(s.substring(word.length()), wordDict, map);
            for (String sub : sublist) 
                res.add(word + (sub.isEmpty() ? "" : " ") + sub);               
        }
    }       
    map.put(s, res);
    return res;
}

Problem solution in C++.

class Solution 
{
public:
    void helper(string &s,string &curr,vector<string>&v,int index,unordered_set<string>&dict)
    {
        if(index==s.length())
        {
            curr.erase(curr.begin()+curr.length()-1);
            v.push_back(curr);
            return;
        }
        string tmp="";
        for(int i=index;i<s.length();i++)
        {
            tmp.push_back(s[i]);
            if(dict.find(tmp)!=dict.end())
            {
                string aux=curr;
                curr+=(tmp+" ");
                helper(s,curr,v,i+1,dict);
                curr=aux;
            }
        }
    }
    vector<string> wordBreak(string s, vector<string>& wordDict) 
    {
        unordered_set<string>dict(wordDict.begin(),wordDict.end());
        vector<string>v;
        string curr="";
        helper(s,curr,v,0,dict);
        return v;
    }
};

coding problems solutions Leetcode Problems Solutions Leetcode

Post navigation

Previous post
Next post

Leave a Reply

Your email address will not be published. Required fields are marked *

Pages

  • About US
  • Contact US
  • Privacy Policy

Follow US

  • YouTube
  • LinkedIn
  • Facebook
  • Pinterest
  • Instagram
©2026 Programmingoneonone | WordPress Theme by SuperbThemes