Word Break - Complete Solution Guide
Word Break is LeetCode problem 139, a Medium level challenge. This complete guide provides step-by-step explanations, multiple solution approaches, and optimized code in python3, java, cpp, c.
Problem Statement
Given a string s and a dictionary of strings wordDict , return true if s can be segmented into a space-separated sequence of one or more dictionary words. Note that the same word in the dictionary may be reused multiple times in the segmentation. Example 1: Input: s = "leetcode", wordDict = ["leet","code"] Output: true Explanation: Return true because "leetcode" can be segmented as "leet code". Example 2: Input: s = "applepenapple", wordDict = ["apple","pen"] Output: true Explanation: Return tru
Detailed Explanation
The Word Break problem asks whether a given string `s` can be segmented into a sequence of one or more words from a given dictionary `wordDict`. The same word in the dictionary can be reused multiple times. The input consists of a string `s` and a list of strings `wordDict`. The output is a boolean value: `true` if `s` can be segmented, and `false` otherwise. For example, if `s` is "leetcode" and `wordDict` is ["leet", "code"], the answer is `true` because "leetcode" can be segmented as "leet code". The constraints include the length of `s` being between 1 and 300, the length of `wordDict` being between 1 and 1000, and the length of each word in `wordDict` being between 1 and 20. All strings consist of lowercase English letters, and the words in `wordDict` are unique.
Solution Approach
The solution employs dynamic programming to solve this problem. A boolean array `dp` of size `n+1` is used, where `dp[i]` is `true` if the substring `s[0...i-1]` can be segmented into words from the `wordDict`, and `false` otherwise. The base case is `dp[0] = true`, indicating that an empty string can be segmented. The algorithm iterates through the string `s`, and for each index `i`, it iterates from `j = max(0, i - max_len)` to `i-1`, where `max_len` is the maximum length of any word in `wordDict`. If `dp[j]` is `true` (meaning `s[0...j-1]` can be segmented) and the substring `s[j...i-1]` is in the `wordDict`, then `dp[i]` is set to `true`, indicating that `s[0...i-1]` can be segmented. The outer loop continues until the end of the string, and the final result is `dp[n]`. This approach correctly determines if the entire string `s` can be segmented into words from `wordDict`.
Step-by-Step Algorithm
- Step 1: Create a set `word_set` from the given `wordDict` for fast lookup.
- Step 2: Initialize a boolean array `dp` of size `n+1` with all values set to `false`, where `n` is the length of the string `s`. Set `dp[0] = true`.
- Step 3: Find the maximum length `max_len` of any word in the `wordDict`. This optimization helps limit the inner loop's iterations.
- Step 4: Iterate through the string `s` from index `i = 1` to `n`.
- Step 5: For each index `i`, iterate from `j = max(0, i - max_len)` to `i - 1`.
- Step 6: If `dp[j]` is `true` (meaning the substring `s[0...j-1]` can be segmented) and the substring `s[j:i]` is present in the `word_set`, then set `dp[i] = true` and break the inner loop.
- Step 7: After the outer loop finishes, return `dp[n]`, which indicates whether the entire string `s` can be segmented.
Key Insights
- Insight 1: Dynamic programming is effective for this problem because we can break down the problem into smaller subproblems. dp[i] represents whether the substring s[0...i-1] can be segmented into words from the dictionary.
- Insight 2: Using a set to store the dictionary words allows for efficient lookups (O(1) on average).
- Insight 3: Optimizing the inner loop by only checking substrings of length up to the maximum length of the words in the dictionary improves performance.
Complexity Analysis
Time Complexity: O(n*m)
Space Complexity: O(n+k)
Topics
This problem involves: Array, Hash Table, String, Dynamic Programming, Trie, Memoization.
Companies
Asked at: Block, Cleartrip, Coupang, CureFit, Flipkart, Grammarly, IBM, Intuit, MongoDB, Netflix, Netskope, Nordstrom, Nutanix, Otter.ai, Palo Alto Networks, Pocket Gems, SAP, Salesforce, Snowflake, TikTok, Uber, Walmart Labs, Yahoo, Zoho, eBay.