Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit ead99fd

Browse files
Merge branch 'smv1999:master' into manny
2 parents 0ca2d96 + c3b61c4 commit ead99fd

File tree

1 file changed

+32
-0
lines changed

1 file changed

+32
-0
lines changed

‎DSA 450 GFG/WordBreak.py‎

Lines changed: 32 additions & 0 deletions
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)

AltStyle によって変換されたページ (->オリジナル) /