Skip to content

Commit dded7b6

Browse files
author
Partho Biswas
committed
139. Word Break
1 parent e0ae1fe commit dded7b6

File tree

2 files changed

+63
-0
lines changed

2 files changed

+63
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -584,6 +584,7 @@ BFS, DFS, Dijkstra, Floyd–Warshall, Bellman-Ford, Kruskal, Prim's, Minimum Spa
584584
|37| [1048. Longest String Chain](https://tinyurl.com/uvb5v6s) | [Python](https://tinyurl.com/wu6rdaw/1048_Longest_String_Chain.py)| [Art 1](https://tinyurl.com/tcslm9l) | Medium | Modified LIS |
585585
|38| **[801. Minimum Swaps To Make Sequences Increasing](https://tinyurl.com/rvtcyvb)** | [Python](https://tinyurl.com/wu6rdaw/801_Minimum_Swaps_To_Make_Sequences_Increasing.py)| [Art 1](https://tinyurl.com/tzx7wpv), [Art 2](https://tinyurl.com/ugybcmz) | Medium | TODO: Check again. Very analytical and tricky to come up with |
586586
|39| **[279. Perfect Squares](https://tinyurl.com/jcjc6kf)** | [Python](https://tinyurl.com/wu6rdaw/279_Perfect_Squares.py)| **[Must](https://tinyurl.com/raomm4y)**, [Vid 1](https://tinyurl.com/rxpaqe8), [Vis 2](https://tinyurl.com/uevqohx) | Medium | TODO: Check again. Very Important. Very analytical and tricky to come up with |
587+
|40| **[139. Word Break](https://tinyurl.com/yyyy9dqz)** | [Python](https://tinyurl.com/wu6rdaw/139_Word_Break.py)| **[Must](https://tinyurl.com/sdzmo32)**, [Vid 1](https://tinyurl.com/s4hvakw), [Vis 2](https://tinyurl.com/rjwjanc), [Vid 3](https://tinyurl.com/rdxmwwg) | Medium | TODO: Check again. Very Important. |
587588

588589

589590
</p>

leetcode.com/python/139_Word_Break.py

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
# Approach 1: Brute Force, Backtracking - TLE
2+
class Solution(object):
3+
def wordBreak(self, s, wordDict):
4+
"""
5+
:type s: str
6+
:type wordDict: List[str]
7+
:rtype: bool
8+
"""
9+
if not s:
10+
return True
11+
canSegment = False
12+
for word in wordDict:
13+
if s.startswith(word):
14+
newS = s[len(word):]
15+
canSegment = self.wordBreak(newS, wordDict)
16+
if canSegment:
17+
break
18+
return canSegment
19+
20+
21+
# Approach 2: Brute Force, Backtracking with memoization - TLE
22+
class Solution(object):
23+
def wordBreak(self, s, wordDict):
24+
"""
25+
:type s: str
26+
:type wordDict: List[str]
27+
:rtype: bool
28+
"""
29+
return self.wordBreakHelper(s, 0, set(wordDict), [False]*len(s))
30+
31+
def wordBreakHelper(self, s, start, wordSet, memo):
32+
if start == len(s):
33+
return True
34+
if memo[start]:
35+
return True
36+
for end in range(start + 1, len(s)):
37+
if s[start:end] in wordSet and self.wordBreakHelper(s, end, wordSet, memo):
38+
memo[start] = True
39+
return memo[start]
40+
else:
41+
memo[start] = False
42+
return memo[start]
43+
44+
45+
46+
47+
# Approach 3: DP
48+
class Solution(object):
49+
def wordBreak(self, s, wordDict):
50+
"""
51+
:type s: str
52+
:type wordDict: List[str]
53+
:rtype: bool
54+
"""
55+
dp = [False for _ in range(len(s) + 1)]
56+
dp[0] = True
57+
for i in range(1, len(s) + 1):
58+
for word in wordDict:
59+
if word == s[i - len(word):i] and dp[i - len(word)]:
60+
dp[i] = True
61+
return dp[-1]
62+

0 commit comments

Comments
 (0)