Tensorum
YouTubeಕಲಿಯಲು ಪ್ರಾರಂಭಿಸಿ
LeetCode 139 · String DP / Word Break

Word Break

LeetCode 139 · ಮಧ್ಯಮ · ಪರಿಹಾರ, intuition ಮತ್ತು ಹಂತ ಹಂತದ ವಾಕ್‌ಥ್ರೂ

dp[end] = any(dp[start] and s[start:end] ∈ words).

ಮಧ್ಯಮArrayHash TableStringDynamic ProgrammingTrie
LeetCode ನಲ್ಲಿ ತೆರೆಯಿರಿ ↗

ನಾನು Word Break (LeetCode 139) ಅನ್ನು ಹೀಗೆ ಪರಿಹರಿಸಿದೆ — String DP / Word Break ಪ್ಯಾಟರ್ನ್‌ನ ಒಂದು ಮಧ್ಯಮ ಪ್ರಾಬ್ಲಮ್. ಕೆಳಗೆ: intuition, ಹಂತ ಹಂತದ approach, ನನ್ನ accepted Python ಕೋಡ್, animated test cases ಮತ್ತು line-by-line debugger.

ಕೆಳಗಿನ ವಿವರಣೆ ಇಂಗ್ಲಿಷ್‌ನಲ್ಲಿದೆ.

Intuition

A string can be segmented if some prefix is a dictionary word and the rest can also be segmented. Rather than re-checking suffixes, mark positions: dp[end] is True when s[:end] can be fully segmented, which happens if some earlier good position start has s[start:end] in the dictionary.

Approach

  1. 1

    Put the dictionary in a set for O(1) lookups.

  2. 2

    dp[0] = True — the empty prefix is trivially segmented.

  3. 3

    For each end, try every start before it; if dp[start] is True and s[start:end] is a word, mark dp[end].

  4. 4

    dp[n] is the answer.

ನಾನು ಹೇಗೆ ಪರಿಹರಿಸಿದೆ · ಮೊದಲ ಆಲೋಚನೆಯಿಂದ ಅಂತಿಮ ಪರಿಹಾರದವರೆಗೆ

1
Recursion on suffixes + memoO(n²) states

lru_cache dfs(word) trying every prefix.

2
Tabulation over prefix endsAcceptedO(n²) time · O(n) space

dp[end] from earlier good starts.

Recursion on suffixes + memoPythonO(n²) states
from functools import lru_cache
class Solution:
    def wordBreak(self, s: str, wordDict: List[str]) -> bool:
        #prefix and sufffix
        words_set = set(wordDict)
        n = len(s)
        @lru_cache(maxsize=None)
        def dfs(word):
            if not word:
                return True
            start=1
            for end in range(start,len(word)+1):
                prefix = word[:end]
                suffix = word[end:]
                if (prefix in words_set and suffix in words_set) or (prefix in words_set and dfs(suffix)):
                    return True
            return False
        return dfs(s)
Tabulation over prefix endsPythonO(n²) time · O(n) space
class Solution:
    def wordBreak(self, s: str, wordDict: List[str]) -> bool:
        #prefix and sufffix
        words_set = set(wordDict)
        n = len(s)
        dp=[False]*(n+1)
        dp[0]=True
        for end in range(1,n+1):
           for start in range(end):
            prefix = s[start:end]
            if prefix in words_set and dp[start]==True:
                dp[end]=True
        return dp[n]
ಸ್ಟೆಪ್-ಥ್ರೂ ಲೋಡ್ ಆಗುತ್ತಿದೆ…

Word Break — ಪದೇ ಪದೇ ಕೇಳುವ ಪ್ರಶ್ನೆಗಳು

Word Break ಹಿಂದಿನ intuition ಏನು?

A string can be segmented if some prefix is a dictionary word and the rest can also be segmented. Rather than re-checking suffixes, mark positions: dp[end] is True when s[:end] can be fully segmented, which happens if some earlier good position start has s[start:end] in the dictionary.

Word Break (LeetCode 139) ಅನ್ನು ಹೇಗೆ ಪರಿಹರಿಸುವುದು?

1. Put the dictionary in a set for O(1) lookups. 2. dp[0] = True — the empty prefix is trivially segmented. 3. For each end, try every start before it; if dp[start] is True and s[start:end] is a word, mark dp[end]. 4. dp[n] is the answer.

Word Break ನ time ಮತ್ತು space complexity ಎಷ್ಟು?

Time: O(n² · L) with substring slicing. Space: O(n). (Accepted approach: Tabulation over prefix ends.)

Word Break ನಲ್ಲಿ ಸಾಮಾನ್ಯ ತಪ್ಪುಗಳು ಯಾವುವು?

Without memo, the recursive version explodes on inputs like 'aaaa…b'. Break once dp[end] is True to save time (optional).

Word Break ಅನ್ನು ಪರಿಹರಿಸಲು ಒಂದಕ್ಕಿಂತ ಹೆಚ್ಚು ಮಾರ್ಗಗಳಿವೆಯೇ?

Recursion on suffixes + memo (O(n²) states): lru_cache dfs(word) trying every prefix. Tabulation over prefix ends (O(n²) time · O(n) space): dp[end] from earlier good starts.