forked from keon/algorithms
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathword_break.py
More file actions
40 lines (31 loc) · 1019 Bytes
/
word_break.py
File metadata and controls
40 lines (31 loc) · 1019 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
"""
Word Break
Given a string and a dictionary of words, determine whether the string
can be segmented into a sequence of dictionary words.
Reference: https://leetcode.com/problems/word-break/
Complexity:
Time: O(n^2)
Space: O(n)
"""
from __future__ import annotations
def word_break(word: str, word_dict: set[str]) -> bool:
"""Determine if word can be segmented into dictionary words.
Args:
word: The string to segment.
word_dict: Set of valid dictionary words.
Returns:
True if word can be segmented, False otherwise.
Examples:
>>> word_break("leetcode", {"leet", "code"})
True
>>> word_break("catsandog", {"cats", "dog", "sand", "and", "cat"})
False
"""
dp_array = [False] * (len(word) + 1)
dp_array[0] = True
for i in range(1, len(word) + 1):
for j in range(0, i):
if dp_array[j] and word[j:i] in word_dict:
dp_array[i] = True
break
return dp_array[-1]