Skip to content

Commit 5d805dd

Browse files
committed
주석 개선
1 parent cad3e9b commit 5d805dd

1 file changed

Lines changed: 42 additions & 33 deletions

File tree

word-break/alphaorderly.py

Lines changed: 42 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,22 @@
11
"""
2-
Time Complexity: O(n * k * m)
2+
Top-down dynamic programming with memoization.
3+
4+
Time Complexity: O(k * n * (n + m)), or O(k * n^2) when m <= n
35
- n = len(s)
4-
- k = number of words in wordDict
5-
- m = average word length in wordDict
6+
- k = len(wordDict)
7+
- m = the maximum word length
68
7-
At each index in s, we may check every word in wordDict, and for each, compare up to m characters.
9+
There are at most n memoized states, and each state checks every word.
10+
In this implementation, s[index:] also creates a suffix whose length is
11+
O(n), while startswith may compare up to m characters.
812
913
Space Complexity: O(n)
10-
- n = len(s), due to recursion stack and memoization table (one entry per possible starting index).
11-
12-
- Uses top-down dynamic programming with memoization (via lru_cache).
13-
- The helper function dp(index) returns True if s[index:] can be fully segmented into words from wordDict.
14-
- Base case: If index == len(s), then the entire string is successfully segmented.
15-
- For each call, iterate through all words in the dictionary and check if s[index:] begins with that word.
16-
- If a match is found, recursively check for the remainder of the string starting after the matched word.
17-
- Return True as soon as any valid segmentation is found.
18-
- Return False if no segmentation is possible for this index.
14+
The memoization table and recursion stack each contain at most n entries.
15+
16+
dp(index) returns whether the suffix beginning at index can be completely
17+
segmented. Reaching len(s) means that all characters have been consumed.
18+
For every dictionary word that matches the current suffix, the function
19+
recursively checks the remaining suffix and stops at the first valid split.
1920
"""
2021
class Solution:
2122
def wordBreak(self, s: str, wordDict: List[str]) -> bool:
@@ -36,22 +37,24 @@ def dp(index: int) -> bool:
3637
return dp(0)
3738

3839
"""
39-
Time Complexity: O(n * k * m)
40+
Bottom-up dynamic programming over prefixes of s.
41+
42+
Time Complexity: O(k * n * (n + m)), or O(k * n^2) when m <= n
4043
- n = len(s)
41-
- k = number of words in wordDict
42-
- m = average word length in wordDict
44+
- k = len(wordDict)
45+
- m = the maximum word length
4346
44-
For every index in s, we consider each word in wordDict and, for each, match up to m characters.
47+
Each of the n + 1 prefix boundaries checks every word. The expression
48+
s[index - W:] creates a suffix of up to O(n) characters, and startswith
49+
may compare up to m characters.
4550
4651
Space Complexity: O(n)
47-
- n = len(s), required for the dp array.
48-
49-
- Uses bottom-up dynamic programming.
50-
- The dp array indicates whether a prefix of s up to index can be segmented into words from wordDict.
51-
- Base case: dp[0] = 1, since the empty string can always be segmented.
52-
- For each index, iterate through all words, and for each, check if s[index - W : index] equals the word, assuming index >= W and dp[index - W] is True.
53-
- If a match is found, update dp[index] to reflect that segmentation.
54-
- Finally, return True if dp[-1] is set, indicating the entire string can be segmented; otherwise, return False.
52+
The dp array stores one value for every prefix boundary.
53+
54+
dp[index] indicates whether s[:index] can be segmented. dp[0] is true because
55+
the empty prefix needs no words. A word of length W can end at index when the
56+
preceding prefix, dp[index - W], is valid and the suffix beginning there starts
57+
with that word. The final entry represents the entire string.
5558
"""
5659
class Solution:
5760
def wordBreak(self, s: str, wordDict: List[str]) -> bool:
@@ -71,18 +74,24 @@ def wordBreak(self, s: str, wordDict: List[str]) -> bool:
7174
return bool(dp[-1])
7275

7376
"""
74-
Time Complexity: O(n * k * m)
77+
Trie-assisted depth-first search over valid word boundaries.
78+
79+
Time Complexity: O(L + n^2)
7580
- n = len(s)
76-
- k = number of words in wordDict
77-
- m = average word length in wordDict
81+
- L = the total number of characters in wordDict
7882
79-
For every index in s, we consider each word in wordDict and, for each, match up to m characters.
83+
Building the trie takes O(L). Each reachable boundary is processed once,
84+
but a trie lookup may scan the remaining suffix, giving O(n^2) total work
85+
in the worst case.
8086
81-
Space Complexity: O(n)
82-
- n = len(s), required for the dp array.
87+
Space Complexity: O(L + n)
88+
The trie uses O(L) space. The visited array, DFS stack, and a lookup's list
89+
of matching end positions use O(n) additional space.
8390
84-
- Uses a trie to store the words in wordDict.
85-
- Returns a list of ending indices of the words that match the prefix (i.e., all indices where a word ends if we start matching from the given index).
91+
startsWith(target, start) follows the trie from target[start:] and returns every
92+
exclusive end index at which a dictionary word finishes. The DFS treats those
93+
indices as the next segmentation boundaries. visited prevents the same boundary
94+
from being added to the stack more than once.
8695
"""
8796
class Trie:
8897
def __init__(self):

0 commit comments

Comments
 (0)