26. Word Break
The problem can be found at the following link: Question Link
Problem Description
You are given a string s
and a list dictionary[]
of words. Your task is to determine whether the string s
can be formed by concatenating one or more words from the dictionary[]
.
Note: From dictionary[]
, any word can be taken any number of times and in any order.
Examples
Example 1:
Input:
s = "ilike"
dictionary[] = ["i", "like", "gfg"]
Output:
true
Explanation:
The given string s
can be broken down as "i like"
using words from the dictionary.
Example 2:
Input:
s = "ilikegfg"
dictionary[] = ["i", "like", "man", "india", "gfg"]
Output:
true
Explanation:
The given string s
can be broken down as "i like gfg"
using words from the dictionary.
Example 3:
Input:
s = "ilikemangoes"
dictionary[] = ["i", "like", "man", "india", "gfg"]
Output:
false
Explanation:
The given string s
cannot be formed using the dictionary words.
Constraints:
$( 1 \leq |s| \leq 3000 )$
$( 1 \leq |dictionary| \leq 1000 )$
$( 1 \leq |dictionary[i]| \leq 100 )$
My Approach
Dynamic Programming
Algorithm Steps:
Use a boolean DP vector
dp
of size n+1 wheredp[i]
indicates if the substrings[0...i-1]
can be segmented.Insert dictionary words into an unordered set for O(1) lookups.
Iterate through the string and check all possible substrings using the dictionary.
If a valid word is found and its prefix was segmentable, mark
dp[i+j]
as true.Return
dp[n]
.
Time and Auxiliary Space Complexity
Expected Time Complexity: $( O(n \times m) )$, as each substring is checked in the dictionary.
Expected Auxiliary Space Complexity: $( O(n) )$, as we use a DP array of size
n+1
.
Code (C++)
class Solution {
public:
bool wordBreak(string s, vector<string>& d) {
unordered_set<string> u(d.begin(), d.end());
int n = s.size(), m = 0;
for(auto &w : d) m = max(m, (int)w.size());
vector<bool> dp(n + 1);
dp[0] = 1;
for(int i = 0; i < n; i++) {
if(!dp[i]) continue;
for(int j = 1; j <= m && i + j <= n; j++)
if(u.count(s.substr(i, j))) dp[i + j] = 1;
}
return dp[n];
}
};
Code (Java)
class Solution {
public boolean wordBreak(String s, String[] d) {
Set<String> set = new HashSet<>(Arrays.asList(d));
int n = s.length(), m = 0;
for (String w : d) m = Math.max(m, w.length());
boolean[] dp = new boolean[n + 1];
dp[0] = true;
for (int i = 0; i < n; i++) {
if (!dp[i]) continue;
for (int j = 1; j <= m && i + j <= n; j++)
if (set.contains(s.substring(i, i + j))) dp[i + j] = true;
}
return dp[n];
}
}
Code (Python)
class Solution:
def wordBreak(self, s, dictionary):
d = set(dictionary)
m = max((len(w) for w in dictionary), default=0)
n = len(s)
dp = [False] * (n + 1)
dp[0] = True
for i in range(n):
if not dp[i]:
continue
for j in range(1, m + 1):
if i + j <= n and s[i:i + j] in d:
dp[i + j] = True
return dp[n]
Contribution and Support:
For discussions, questions, or doubts related to this solution, feel free to connect on LinkedIn: Any Questions. Letβs make this learning journey more collaborative!
β If you find this helpful, please give this repository a star! β
πVisitor Count
Last updated