Cover image for Interview Classic 150 Questions P139 Word Break

Interview Classic 150 Questions P139 Word Break


Timeline

Timeline

2025-12-13

init

Dynamic Programming

Problem:

dp[i] indicates whether the prefix s[0…i-1] can be successfully split into words from the dictionary. dp[0] = true, meaning the empty string can be split.
dp[The condition for i] to be true is that dp[j](0 <= j < i)is true, and s.substr(j, i-j) is a word in wordDict.

12345678910111213141516171819202122232425262728
#include <string>#include <vector>#include <unordered_set>using std::vector;using std::string;using std::unordered_set;class Solution {    public:        bool wordBreak(string s, vector<string> &wordDict)        {                int i, j, n = s.size();                unordered_set<string> dict(wordDict.begin(), wordDict.end());                // dp[i] indicates whether the prefix s[0..i-1] can be successfully split                vector<bool> dp(n + 1, false);                dp[0] = true; // The empty string can be split                for (i = 1; i <= n; i++) {                        for (j = 0; j < i; j++) { // s[0..i-1]                                //                                if (dp[j] && dict.count(s.substr(j, i - j))) {                                        dp[i] = true;                                        break;                                }                        }                }                return dp[n];        }};

LeetCode Hot 100 rewrite: used prefix tree + BFS approach

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
#include <string>#include <vector>#include <queue>using std::vector;using std::string;using std::queue;struct TrieNode {        vector<TrieNode *> children;        char data;        bool is_end;        TrieNode()        {                this->children = vector<TrieNode *>(26, nullptr);                this->is_end = false;        }};class Solution {    public:        bool wordBreak(string s, vector<string> &wordDict)        {                // 1 <= s.length <= 300                // 1 <= wordDict.length <= 1000                // 1 <= wordDict[i].length <= 20                // s and wordDict[i] consist only of lowercase English letters.                // All strings in wordDict are distinct.                TrieNode *root = new TrieNode, *p;                int i, n = s.size();                for (string word : wordDict) {                        p = root;                        for (char ch : word) {                                if (p->children[ch - 'a'] == nullptr)                                        p->children[ch - 'a'] = new TrieNode;                                p = p->children[ch - 'a'];                                p->data = ch;                        }                        p->is_end = true;                }                queue<int> que;                vector<bool> visited(n, false);                que.push(0);                visited[0] = true;                while (!que.empty()) {                        int start = que.front();                        que.pop();                        if (start == n)                                return true;                        p = root;                        for (i = start; i < n; i++) {                                if (p->children[s[i] - 'a'] == nullptr)                                        break;                                p = p->children[s[i] - 'a'];                                if (p->is_end && !visited[i + 1]) {                                        que.push(i + 1);                                        visited[i + 1] = true;                                }                        }                }                return false;        }};
Loading comments…