|
| 1 | +Given a non-empty string s and a dictionary wordDict containing a list of non-empty words, |
| 2 | +determine if s can be segmented into a space-separated sequence of one or more dictionary words. |
| 3 | + |
| 4 | +Note: |
| 5 | + |
| 6 | +The same word in the dictionary may be reused multiple times in the segmentation. |
| 7 | +You may assume the dictionary does not contain duplicate words. |
| 8 | +Example 1: |
| 9 | + |
| 10 | +Input: s = "leetcode", wordDict = ["leet", "code"] |
| 11 | +Output: true |
| 12 | +Explanation: Return true because "leetcode" can be segmented as "leet code". |
| 13 | +Example 2: |
| 14 | + |
| 15 | +Input: s = "applepenapple", wordDict = ["apple", "pen"] |
| 16 | +Output: true |
| 17 | +Explanation: Return true because "applepenapple" can be segmented as "apple pen apple". |
| 18 | + Note that you are allowed to reuse a dictionary word. |
| 19 | +Example 3: |
| 20 | + |
| 21 | +Input: s = "catsandog", wordDict = ["cats", "dog", "sand", "and", "cat"] |
| 22 | +Output: false |
| 23 | + |
| 24 | + |
| 25 | + |
| 26 | +// Trie (TLE) |
| 27 | + |
| 28 | +class Solution { |
| 29 | +public: |
| 30 | + |
| 31 | + struct TrieNode { |
| 32 | + bool ew; // end of word |
| 33 | + struct TrieNode* child[26]; |
| 34 | + }; |
| 35 | + |
| 36 | + TrieNode* createNode(){ |
| 37 | + TrieNode* newNode= new TrieNode; |
| 38 | + newNode->ew=false; |
| 39 | + for(int i=0;i<26;i++) |
| 40 | + newNode->child[i]=NULL; |
| 41 | + return newNode; |
| 42 | + } |
| 43 | + |
| 44 | + |
| 45 | + void insert(string word, TrieNode* root){ |
| 46 | + TrieNode* curr=root; |
| 47 | + int wordLen=word.length(); |
| 48 | + for(int i=0;i<wordLen;i++){ |
| 49 | + int index=word[i]-'a'; |
| 50 | + if(curr->child[index]==NULL) |
| 51 | + curr->child[index]=createNode(); |
| 52 | + curr=curr->child[index]; |
| 53 | + } |
| 54 | + curr->ew=true; |
| 55 | + } |
| 56 | + |
| 57 | + bool search(string word, TrieNode* root){ |
| 58 | + TrieNode* curr=root; |
| 59 | + int wordLen=word.length(); |
| 60 | + for(int i=0;i<wordLen;i++){ |
| 61 | + int index=word[i]-'a'; |
| 62 | + if(curr->child[index]==NULL) return false; |
| 63 | + curr=curr->child[index]; |
| 64 | + } |
| 65 | + return (curr!=NULL && curr->ew); |
| 66 | + |
| 67 | + } |
| 68 | + |
| 69 | + bool wordBreakUtil(string s, TrieNode* root){ |
| 70 | + int size=s.length(); |
| 71 | + if(size==0) return true; |
| 72 | + |
| 73 | + for(int i=1;i<=size;i++){ |
| 74 | + if( search(s.substr(0, i), root) && wordBreakUtil(s.substr(i, size-i), root )) |
| 75 | + return true; |
| 76 | + } |
| 77 | + return false; |
| 78 | + } |
| 79 | + |
| 80 | + bool wordBreak(string s, vector<string>& wordDict) { |
| 81 | + int n=wordDict.size(); |
| 82 | + TrieNode* root= createNode(); |
| 83 | + for(int i=0;i<n;i++){ |
| 84 | + insert(wordDict[i], root); |
| 85 | + } |
| 86 | + |
| 87 | + return wordBreakUtil(s, root); |
| 88 | + } |
| 89 | +}; |
| 90 | + |
| 91 | + |
| 92 | + |
| 93 | + |
| 94 | + |
| 95 | + |
| 96 | + |
| 97 | + |
| 98 | + |
| 99 | + |
| 100 | + |
| 101 | + |
| 102 | + |
| 103 | + |
| 104 | + |
| 105 | +// Recursive (TLE) |
| 106 | + |
| 107 | +class Solution { |
| 108 | +public: |
| 109 | + |
| 110 | + bool searchDict(string word, vector<string> &wordDict){ |
| 111 | + for(auto &x: wordDict) |
| 112 | + if(word==x) |
| 113 | + return true; |
| 114 | + return false; |
| 115 | + } |
| 116 | + |
| 117 | + bool wordBreakUtil(string s, vector<string> &wordDict){ |
| 118 | + int size=s.length(); |
| 119 | + if(size==0) return true; |
| 120 | + for(int i=1;i<=size;i++){ |
| 121 | + if(searchDict(s.substr(0, i), wordDict) && wordBreakUtil(s.substr(i, size-i), wordDict) ) |
| 122 | + return true; |
| 123 | + } |
| 124 | + return false; |
| 125 | + } |
| 126 | + |
| 127 | + bool wordBreak(string s, vector<string>& wordDict) { |
| 128 | + return wordBreakUtil(s, wordDict); |
| 129 | + } |
| 130 | +}; |
| 131 | + |
| 132 | + |
| 133 | + |
| 134 | + |
| 135 | + |
| 136 | + |
| 137 | +// Recursive (memoized) |
| 138 | + |
| 139 | +class Solution { |
| 140 | +public: |
| 141 | + |
| 142 | + unordered_map<string, bool> memo; |
| 143 | + |
| 144 | + bool searchDict(string word, vector<string> &wordDict){ |
| 145 | + for(auto &x: wordDict) |
| 146 | + if(word==x) |
| 147 | + return true; |
| 148 | + return false; |
| 149 | + } |
| 150 | + |
| 151 | + bool wordBreakUtil(string s, vector<string> &wordDict){ |
| 152 | + int size=s.length(); |
| 153 | + if(size==0) return true; |
| 154 | + if(memo.find(s)!=memo.end()) return memo[s]; |
| 155 | + for(int i=1;i<=size;i++){ |
| 156 | + if(searchDict(s.substr(0, i), wordDict) && wordBreakUtil(s.substr(i, size-i), wordDict) ) |
| 157 | + return memo[s]=true; |
| 158 | + } |
| 159 | + return memo[s]=false; |
| 160 | + } |
| 161 | + |
| 162 | + bool wordBreak(string s, vector<string>& wordDict) { |
| 163 | + return wordBreakUtil(s, wordDict); |
| 164 | + } |
| 165 | +}; |
| 166 | + |
| 167 | + |
| 168 | + |
| 169 | + |
| 170 | +// Dynamic Programming |
| 171 | + |
| 172 | +// dp[i] denotes that the s.substr(0,i) is available in our dictionary |
| 173 | + |
| 174 | +class Solution { |
| 175 | +public: |
| 176 | + bool wordBreak(string s, vector<string>& wordDict) { |
| 177 | + set<string>dictSet; |
| 178 | + for(auto &x: wordDict) |
| 179 | + dictSet.insert(x); |
| 180 | + |
| 181 | + int n=s.length(); |
| 182 | + |
| 183 | + vector<bool> dp(n+1, false); |
| 184 | + |
| 185 | + dp[0]=true; |
| 186 | + |
| 187 | + for(int len=1;len<=n;len++){ |
| 188 | + for(int i=0;i<len;i++){ |
| 189 | + if(dp[i] && dictSet.find(s.substr(i, len-i))!=dictSet.end()){ |
| 190 | + dp[len]=true; |
| 191 | + break; |
| 192 | + } |
| 193 | + } |
| 194 | + } |
| 195 | + |
| 196 | + return dp[n]; |
| 197 | + } |
| 198 | +}; |
0 commit comments