Complete the code to check if the input string is empty.
if (s.[1]()) { return true; }
The empty() function returns true if the string has no characters, which means the string is empty.
Complete the code to iterate over the string length for dynamic programming.
for (int i = 1; i <= s.[1](); i++) { // logic }
The size() function returns the number of characters in the string, which is used to control the loop.
Fix the error in substring extraction for checking dictionary words.
string word = s.substr(j, [1]);j - i which can be negative.The substring length should be i - j because i is the current end index and j is the start index.
Fill both blanks to correctly update the DP array when a word is found.
if (dp[[1]] && dict.find(word) != dict.[2]()) { dp[i] = true; }
i instead of j for DP check.count() which returns the number of elements, not an iterator.The DP array is checked at index j to see if the substring before j can be segmented. The dictionary's end() function is used to check if the word is present.
Fill all three blanks to complete the word break function using DP and dictionary lookup.
bool wordBreak(string s, unordered_set<string>& dict) {
vector<bool> dp(s.[1]() + 1, false);
dp[0] = true;
for (int i = 1; i <= s.[2](); i++) {
for (int j = 0; j < i; j++) {
string word = s.substr(j, i - j);
if (dp[j] && dict.find(word) != dict.[3]()) {
dp[i] = true;
break;
}
}
}
return dp[s.size()];
}size() and length().begin() instead of end() for dictionary lookup.The DP vector size is set using size(). The outer loop uses length() to iterate through the string. The dictionary's end() function is used to check if the word is not found.
