|
| 1 | +// A Dynamic Programming based program to test whether a given string can be segmented into space separated words in dictionary |
| 2 | + |
| 3 | + |
| 4 | +#include <bits/stdc++.h> |
| 5 | +using namespace std; |
| 6 | + |
| 7 | +/* A utility function to check whether a word |
| 8 | +is present in dictionary or not. An array of |
| 9 | +strings is used for dictionary. Using array |
| 10 | +of strings for dictionary is definitely not |
| 11 | +a good idea. We have used for simplicity of |
| 12 | +the program*/ |
| 13 | +int dictionaryContains(string word) |
| 14 | +{ |
| 15 | + |
| 16 | +//intialization of dictionary |
| 17 | + string dictionary[] = { "mobile", "samsung", "sam", "sung", "man", "mango", "icecream", "and", "go", "i", "like", "ice", "cream" }; |
| 18 | + |
| 19 | + int size = sizeof(dictionary) / sizeof(dictionary[0]); |
| 20 | + for (int i = 0; i < size; i++) |
| 21 | + if (dictionary[i].compare(word) == 0) |
| 22 | + return true; |
| 23 | + return false; |
| 24 | +} |
| 25 | + |
| 26 | +// Returns true if string can be segmented into space |
| 27 | +// separated words, otherwise returns false |
| 28 | +bool wordBreak(string s) |
| 29 | +{ |
| 30 | + int n = s.size(); |
| 31 | + if (n == 0) |
| 32 | + return true; |
| 33 | + |
| 34 | + // Create the DP table to store results of subroblems. |
| 35 | + // The value dp[i] will be true if str[0..i] can be |
| 36 | + // segmented into dictionary words, otherwise false. |
| 37 | + vector<bool> dp(n + 1, 0); // Initialize all values |
| 38 | + // as false. |
| 39 | + |
| 40 | + // matched_index array represents the indexes for which |
| 41 | + // dp[i] is true. Initially only -1 element is present |
| 42 | + // in this array. |
| 43 | + vector<int> matched_index; |
| 44 | + matched_index.push_back(-1); |
| 45 | + |
| 46 | + for (int i = 0; i < n; i++) { |
| 47 | + int msize = matched_index.size(); |
| 48 | + |
| 49 | + // Flag value which tells that a substring matches |
| 50 | + // with given words or not. |
| 51 | + int f = 0; |
| 52 | + |
| 53 | + // Check all the substring from the indexes matched |
| 54 | + // earlier. If any of that substring matches than |
| 55 | + // make flag value = 1; |
| 56 | + for (int j = msize - 1; j >= 0; j--) { |
| 57 | + |
| 58 | + // sb is substring starting from matched_index[j] |
| 59 | + // + 1 and of length i - matched_index[j] |
| 60 | + string sb = s.substr(matched_index[j] + 1, i - matched_index[j]); |
| 61 | + |
| 62 | + if (dictionaryContains(sb)) { |
| 63 | + f = 1; |
| 64 | + break; |
| 65 | + } |
| 66 | + } |
| 67 | + |
| 68 | + // If substring matches than do dp[i] = 1 and |
| 69 | + // push that index in matched_index array. |
| 70 | + if (f == 1) { |
| 71 | + dp[i] = 1; |
| 72 | + matched_index.push_back(i); |
| 73 | + } |
| 74 | + } |
| 75 | + return dp[n - 1]; |
| 76 | +} |
| 77 | + |
| 78 | +// Driver code |
| 79 | +int main() |
| 80 | +{ |
| 81 | +//test cases |
| 82 | + wordBreak("ilikesamsung") ? cout << "Yes\n" : cout << "No\n"; |
| 83 | + wordBreak("iiiiiiii") ? cout << "Yes\n" : cout << "No\n"; |
| 84 | + wordBreak("") ? cout << "Yes\n" : cout << "No\n"; |
| 85 | + wordBreak("ilikelikeimangoiii") ? cout << "Yes\n" : cout << "No\n"; |
| 86 | + wordBreak("samsungandmango") ? cout << "Yes\n" : cout << "No\n"; |
| 87 | + wordBreak("samsungandmangok") ? cout << "Yes\n" : cout << "No\n"; |
| 88 | + return 0; |
| 89 | +} |
0 commit comments