Word Break ii | LeetCode 140 | C++, Python
Vložit
- čas přidán 29. 07. 2020
- LeetCode Solutions: • LeetCode Solutions | L...
June LeetCoding Challenge: • Playlist
May LeetCoding Challenge: • Playlist
Github Link: github.com/KnowledgeCenterYou...
*** Best Books For Data Structures & Algorithms for Interviews:*********
1. Cracking the Coding Interview: amzn.to/2WeO3eO
2. Cracking the Coding Interview Paperback: amzn.to/3aSSe3Q
3. Coding Interview Questions - Narasimha Karumanchi: amzn.to/3cYqjkV
4. Data Structures and Algorithms Made Easy - N. Karumanchi: amzn.to/2U8FrDt
5. Data Structures & Algorithms made Easy in Java - N. Karumanchi: amzn.to/2U0qZgY
6. Introduction to Algorithms - CLR - Cormen, Leiserson, Rivest: amzn.to/2Wdp8rZ
*****************************************************************************
July LeetCoding Challenge | Problem 30 | Word Break ii | 30 July,
Facebook Coding Interview question,
google coding interview question,
leetcode,
Word Break ii,
Word Break ii c++,
Word Break ii Java,
Word Break ii python,
Word Break ii solution,
140. Word Break ii,
#CodingInterview #LeetCode #JulyLeetCodingChallenge #Google #Amazon #WordBreak
Thanks. Out of all the solutions on youtube, yours was the most understandable!
Great to hear!
Can you solve it from bottom-up approach? and talk about the difference btw top-down and bottom-up?
Neatly explained. .Thanks
Thank you a lot, Sir! Could you please also explain how to solve it without memoization, if possible?
Thanks!
one of the best explanation of this problem. Thnx!
Glad you liked it!
Is the TC for this: O(len(s) * len(wordDict)) ?
Best solution in youtube ,very simple
Glad to hear.
what is the time complexity of the solution
Thank you for the explanation.
Glad it was helpful!
genius code!
Thank you for the solution for this! One question though: what is the time and space complexity of the solution above? (with and without caching/memoization)
Thanks again
I have the same question !?
did you find?
Thank you, great explanation!What would the time and space complexity be for this problem?
Sir please solve reorder data in log files leetcode problem
Can Trie be used here?
fair question -- it will exceed the time-limit, because the trie saves you space, but at what cost? that cost is time. you still have to perform linear scan on substrings. the hashmap/dictionary reduces substring postfix look-ups to O(1)
Please do word break i too.
Your solution is not submitting on interviewbit for large inputs.
Your submission failed for the following input:
A : "aabbbabaaabbbabaabaab"
B : [ "bababbbb", "bbbabaa", "abbb", "a", "aabbaab", "b", "babaabbbb", "aa", "bb" ]
I also have same case...
What is the time complexity of the recursive solution , without DP / Memoization ??
did you find?
n^2 * 2^n
Java Solution
class Solution {
Map memo = new HashMap();
public List wordBreak(String s, List wordDict) {
if (memo.containsKey(s)) return memo.get(s);
List res = new ArrayList();
for (String word : wordDict) {
if (s.startsWith(word)) {
if (s.length() == word.length())
res.add(word);
else {
List sub = wordBreak(s.substring(word.length()), wordDict);
for (String w : sub)
res.add(word + " " + w);
}
}
}
memo.put(s, res);
return memo.get(s);
}
}
If looking for Java Solution,
class Solution {
HashMap map = new HashMap();
public List wordBreak(String s, List wordDict) {
List result = new ArrayList();
if (map.containsKey(s))
return map.get(s);
for (String w : wordDict) {
if (w.length() > s.length())
continue;
String firstWord = s.substring(0, w.length());
if (firstWord.equals(w)) {
if (s.length() == w.length()) {
result.add(0, w);
} else {
String leftOne = s.substring(w.length(), s.length());
List tmp = wordBreak(leftOne, wordDict);
for (String val : tmp) {
String sb = w + " " + val;
result.add(0, sb);
}
}
}
}
map.put(s, result);
return result;
}
}
Java Code:
class Solution {
Map < String, List < String >> dp = new HashMap();
public List < String > wordBreak(String s, List < String > wordDict) {
if (dp.containsKey(s))
return dp.get(s);
List < String > result = new ArrayList();
for (String w: wordDict) {
if (s.length() >= w.length() && s.substring(0, w.length()).equals(w)) {
if (w.length() == s.length()) {
result.add(w);
} else {
List < String > tmp = wordBreak(s.substring(w.length()), wordDict);
for (String t: tmp) {
result.add(w + " " + t);
}
}
}
}
dp.put(s, result);
return result;
}
}
it would be great if you clearly dry the recursive solution instead of just writing random things