Skip to content

Commit c3b61c4

Browse files
authored
Merge pull request #926 from dsrao711/issue_925
Python soln for Word Break - DP approach
2 parents 98db044 + 8db019c commit c3b61c4

File tree

1 file changed

+32
-0
lines changed

1 file changed

+32
-0
lines changed

Diff for: DSA 450 GFG/WordBreak.py

+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
2+
# Link : https://leetcode.com/problems/word-break/submissions/
3+
4+
# Reference : https://www.youtube.com/watch?v=Sx9NNgInc3A
5+
6+
# TC : O(mn)
7+
8+
# Approach : https://somber-approval-8f1.notion.site/DSA-Solutions-34100a8ab92f42029011dcf591668343
9+
10+
11+
class Solution(object):
12+
def wordBreak(self, s, wordDict):
13+
"""
14+
:type s: str
15+
:type wordDict: List[str]
16+
:rtype: bool
17+
"""
18+
19+
dp = [False] * (len(s) + 1)
20+
dp[len(s)] = True
21+
22+
for i in range(len(s) - 1 , -1 , -1):
23+
for w in wordDict:
24+
if((i + len(w)) <= len(s) and s[i : i + len(w)] == w):
25+
dp[i] = dp[i + len(w)]
26+
27+
if(dp[i]):
28+
break
29+
30+
return dp[0]
31+
32+

0 commit comments

Comments
 (0)