The title is as follows :

Given a string s And a set of words dict, judge s Is it possible to split a word sequence with spaces , Make all words in the word sequence dict Words in ( A sequence can contain one or more words ).

for example :
given s=“leetcode”;
dict=["leet", "code"].
return true, because "leetcode" Can be divided into "leet code".
 

Given a string s and a dictionary of words dict, determine if s can be
segmented into a space-separated sequence of one or more dictionary words.

For example, given

s ="leetcode",
dict =["leet", "code"].

Return true because"leetcode"can be segmented as"leet code".

This problem can be solved by dynamic programming , Because I don't know how many words this word is made up of , So we turn this problem into n A small problem , Then deduce the following answers one by one with small questions .
class Solution { public: bool wordBreak(string s, unordered_set<string> &dict)
{ if (s.empty()){ return false; } if (dict.empty()){ return false; }
vector<bool>a(s.size()+1, 0); a[0] = true; for (int i = 1; i<s.size()+1; i++){
for (int j = 0; j<i; j++){ if (a[j]){ if (dict.count(s.substr(j, i - j))){ a[i]
= true; break; } } } } return a[s.size()]; } };
 

Technology
Daily Recommendation