<p>Given a string <code>s</code> and a dictionary of strings <code>wordDict</code>, return <code>true</code> if <code>s</code> can be segmented into a space-separated sequence of one or more dictionary words.</p>

<p><strong>Note</strong> that the same word in the dictionary may be reused multiple times in the segmentation.</p>

<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>

<pre>
<strong>Input:</strong> s = &quot;leetcode&quot;, wordDict = [&quot;leet&quot;,&quot;code&quot;]
<strong>Output:</strong> true
<strong>Explanation:</strong> Return true because &quot;leetcode&quot; can be segmented as &quot;leet code&quot;.
</pre>

<p><strong class="example">Example 2:</strong></p>

<pre>
<strong>Input:</strong> s = &quot;applepenapple&quot;, wordDict = [&quot;apple&quot;,&quot;pen&quot;]
<strong>Output:</strong> true
<strong>Explanation:</strong> Return true because &quot;applepenapple&quot; can be segmented as &quot;apple pen apple&quot;.
Note that you are allowed to reuse a dictionary word.
</pre>

<p><strong class="example">Example 3:</strong></p>

<pre>
<strong>Input:</strong> s = &quot;catsandog&quot;, wordDict = [&quot;cats&quot;,&quot;dog&quot;,&quot;sand&quot;,&quot;and&quot;,&quot;cat&quot;]
<strong>Output:</strong> false
</pre>

<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>

<ul>
	<li><code>1 &lt;= s.length &lt;= 300</code></li>
	<li><code>1 &lt;= wordDict.length &lt;= 1000</code></li>
	<li><code>1 &lt;= wordDict[i].length &lt;= 20</code></li>
	<li><code>s</code> and <code>wordDict[i]</code> consist of only lowercase English letters.</li>
	<li>All the strings of <code>wordDict</code> are <strong>unique</strong>.</li>
</ul>

class Solution {
public:
    bool findAns(string &s, unordered_set<string> &wordSet, string &str, unordered_map<string, bool> &mp) {
        // If the current string is equal to the target string
        if (str == s) return true;

        // If the current string length exceeds the target string length
        if (str.size() > s.size()) return false;

        // If the current string has been computed before, return the stored result
        if (mp.find(str) != mp.end()) return mp[str];

        // Try appending each word from the dictionary
        for (const string &word : wordSet) {
            string newStr = str + word;

            // Check if the new string is a prefix of the target string
            if (s.substr(0, newStr.size()) == newStr) {
                // Recursively check if appending this word leads to a solution
                if (findAns(s, wordSet, newStr, mp)) {
                    return mp[str] = true; // Memoize and return true if a solution is found
                }
            }
        }

        // Memoize and return false if no solution is found
        return mp[str] = false;
    }

    bool wordBreak(string s, vector<string> &wordDict) {
        unordered_set<string> wordSet(wordDict.begin(), wordDict.end()); // Use a set for faster lookups
        unordered_map<string, bool> mp; // Memoization map
        string str = ""; // Start with an empty string
        return findAns(s, wordSet, str, mp);
    }
};

15033