|
| 1 | +Given a non-empty string s and a dictionary wordDict containing a list of non-empty words, add spaces in s to construct a sentence where each word is a valid dictionary word. Return all such possible sentences. |
| 2 | + |
| 3 | +Note: |
| 4 | + |
| 5 | +The same word in the dictionary may be reused multiple times in the segmentation. |
| 6 | +You may assume the dictionary does not contain duplicate words. |
| 7 | +Example 1: |
| 8 | + |
| 9 | +Input: |
| 10 | +s = "catsanddog" |
| 11 | +wordDict = ["cat", "cats", "and", "sand", "dog"] |
| 12 | +Output: |
| 13 | +[ |
| 14 | + "cats and dog", |
| 15 | + "cat sand dog" |
| 16 | +] |
| 17 | +Example 2: |
| 18 | + |
| 19 | +Input: |
| 20 | +s = "pineapplepenapple" |
| 21 | +wordDict = ["apple", "pen", "applepen", "pine", "pineapple"] |
| 22 | +Output: |
| 23 | +[ |
| 24 | + "pine apple pen apple", |
| 25 | + "pineapple pen apple", |
| 26 | + "pine applepen apple" |
| 27 | +] |
| 28 | +Explanation: Note that you are allowed to reuse a dictionary word. |
| 29 | +Example 3: |
| 30 | + |
| 31 | +Input: |
| 32 | +s = "catsandog" |
| 33 | +wordDict = ["cats", "dog", "sand", "and", "cat"] |
| 34 | +Output: |
| 35 | +[] |
| 36 | + |
| 37 | + |
| 38 | + |
| 39 | + |
| 40 | + |
| 41 | + |
| 42 | + |
| 43 | + |
| 44 | + |
| 45 | +string s;int n; |
| 46 | +unordered_map<string, int> mp; |
| 47 | + |
| 48 | +bool cut(int start, int end, string a,vector<vector<char> >& dp) |
| 49 | +{ |
| 50 | + if(start > end) |
| 51 | + return 1; |
| 52 | + |
| 53 | + if(dp[start][end] != '?') |
| 54 | + { |
| 55 | + if(dp[start][end] == '1') |
| 56 | + return 1; |
| 57 | + return 0; |
| 58 | + } |
| 59 | + string ans; |
| 60 | + |
| 61 | + for(int i = start; i <= end; i++) |
| 62 | + { |
| 63 | + ans += a[i]; |
| 64 | + if(mp.find(ans) != mp.end()) |
| 65 | + if(cut(i+1, end, a, dp)) |
| 66 | + { |
| 67 | + dp[start][end] = '1'; |
| 68 | + return 1; |
| 69 | + } |
| 70 | + } |
| 71 | + dp[start][end] = '0'; |
| 72 | + return 0; |
| 73 | +} |
| 74 | + |
| 75 | +void backtrack(string s, vector<string> &ans, string yet, int idx) |
| 76 | +{ |
| 77 | + if(idx == n) |
| 78 | + { |
| 79 | + yet.pop_back(); |
| 80 | + ans.push_back(yet); |
| 81 | + return; |
| 82 | + } |
| 83 | + string temp = ""; |
| 84 | + for(int i = idx; i < n; i++) |
| 85 | + { |
| 86 | + temp += s[i]; |
| 87 | + if(mp.find(temp) != mp.end()) |
| 88 | + { |
| 89 | + string ss = yet; |
| 90 | + ss += temp; |
| 91 | + ss += " "; |
| 92 | + backtrack(s, ans, ss, i+1); |
| 93 | + } |
| 94 | + } |
| 95 | +} |
| 96 | + |
| 97 | +class Solution { |
| 98 | +public: |
| 99 | + vector<string> wordBreak(string A, vector<string>& B) { |
| 100 | + vector<string> ans; |
| 101 | + mp.clear(); |
| 102 | + s = A; |
| 103 | + n = A.size(); |
| 104 | + for(int i = 0; i < B.size(); i++) |
| 105 | + mp[B[i]] = 1; |
| 106 | + vector<vector<char> > dp(n, vector<char>(n, '?')); |
| 107 | + int x = cut(0, n-1, A, dp); |
| 108 | + if(x == 0) |
| 109 | + return ans; |
| 110 | + backtrack(A, ans, "", 0); |
| 111 | + return ans; |
| 112 | + } |
| 113 | +}; |
0 commit comments