**LeetCode Problem | LeetCode Problems For Beginners | LeetCode Problems & Solutions | Improve Problem Solving Skills | LeetCode Problems Java | LeetCode Solutions in C++**

Hello **Programmers/Coders,** Today we are going to share ** solutions to the Programming problems of LeetCode Solutions in C++, Java, & Python**. At Each Problem with Successful submission with

**all Test Cases Passed,**you will get a score or marks and LeetCode Coins. And after solving maximum problems, you will be getting stars. This will highlight your profile to the recruiters.

In this post, you will find the solution for the **Word Break** **in C++, Java & Python-LeetCode problem**. We are providing the **correct and tested solutions** to coding problems present on **LeetCode**. If you are not able to solve any problem, then you can take help from our Blog/website.

Use “Ctrl+F” To Find Any Questions Answer. & For Mobile User, You Just Need To Click On Three dots In Your Browser & You Will Get A “Find” Option There. Use These Option to Get Any Random Questions Answer.

**About LeetCode**

*LeetCode* is one of the most well-known online judge platforms to help you enhance your skills, expand your knowledge and prepare for technical interviews.

LeetCode is for **software engineers who are looking to practice technical questions and advance their skills**. Mastering the questions in each level on LeetCode is a good way to prepare for technical interviews and keep your skills sharp. They also have a repository of solutions with the reasoning behind each step.

LeetCode has over 1,900 questions for you to practice, covering many different programming concepts. Every coding problem has a classification of either *Easy*, *Medium*, or *Hard*.

**LeetCode problems focus on algorithms and data structures. Here is some topic you can find problems on LeetCode:**

- Mathematics/Basic Logical Based Questions
- Arrays
- Strings
- Hash Table
- Dynamic Programming
- Stack & Queue
- Trees & Graphs
- Greedy Algorithms
- Breadth-First Search
- Depth-First Search
- Sorting & Searching
- BST (Binary Search Tree)
- Database
- Linked List
- Recursion, etc.

Leetcode has a huge number of test cases and questions from interviews too like Google, Amazon, Microsoft, Facebook, Adobe, Oracle, Linkedin, Goldman Sachs, etc. LeetCode helps you in getting a job in Top MNCs. To crack FAANG Companies, LeetCode problems can help you in building your logic.

** Link for the Problem** – Word Break– LeetCode Problem

Word Break– LeetCode Problem

**Problem:**

Given a string `s`

and a dictionary of strings `wordDict`

, return `true`

if `s`

can be segmented into a space-separated sequence of one or more dictionary words.

**Note** that the same word in the dictionary may be reused multiple times in the segmentation.

**Example 1:**

Input:s = "leetcode", wordDict = ["leet","code"]Output:trueExplanation:Return true because "leetcode" can be segmented as "leet code".

**Example 2:**

Input:s = "applepenapple", wordDict = ["apple","pen"]Output:trueExplanation:Return true because "applepenapple" can be segmented as "apple pen apple". Note that you are allowed to reuse a dictionary word.

**Example 3:**

Input:s = "catsandog", wordDict = ["cats","dog","sand","and","cat"]Output:false

**Constraints:**

`1 <= s.length <= 300`

`1 <= wordDict.length <= 1000`

`1 <= wordDict[i].length <= 20`

`s`

and`wordDict[i]`

consist of only lowercase English letters.- All the strings of
`wordDict`

are**unique**.

Template – LeetCode Solutions

Word Break Solution in C++:

class Solution { public: bool wordBreak(string s, vector<string>& wordDict) { const int n = s.length(); unordered_set<string> wordSet{begin(wordDict), end(wordDict)}; vector<bool> dp(n + 1); // dp[i] := true if s[0..i) can be segmented dp[0] = true; for (int i = 1; i <= n; ++i) for (int j = 0; j < i; ++j) // s[0..j) can be segmented and s[j..i) in wordSet // so s[0..i) can be segmented if (dp[j] && wordSet.count(s.substr(j, i - j))) { dp[i] = true; break; } return dp[n]; } };

Word Break Solution in Java:

class Solution { public boolean wordBreak(String s, List<String> wordDict) { final int n = s.length(); Set<String> wordSet = new HashSet<>(wordDict); boolean[] dp = new boolean[n + 1]; // dp[i] := true if s[0..i) can be segmented dp[0] = true; for (int i = 1; i <= n; ++i) for (int j = 0; j < i; ++j) // s[0..j) can be segmented and s[j..i) in wordSet // so s[0..i) can be segmented if (dp[j] && wordSet.contains(s.substring(j, i))) { dp[i] = true; break; } return dp[n]; } }

Word Break Solution in Python:

class Solution: def wordBreak(self, s: str, wordDict: List[str]) -> bool: n = len(s) wordSet = set(wordDict) dp = [True] + [False] * n # dp[i] := True if s[0..i) can be segmented for i in range(1, n + 1): for j in range(i): # s[0..j) can be segmented and s[j..i) in wordSet # so s[0..i) can be segmented if dp[j] and s[j:i] in wordSet: dp[i] = True break return dp[n]