<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 Node {
public:
    char ch;
    Node* children[26];
    bool isEnd;

    Node(char x = '/') {  // Default constructor for root node
        this->ch = x;
        this->isEnd = false;
        for (int i = 0; i < 26; i++) {
            this->children[i] = NULL;
        }
    }
};

class Solution {

public:
    // Time Complexity: O(M*N), Space Complexity: O(M*N), where M is the length of the longest string and N is the number of strings.
    Node* root = new Node();  // Initialize root node

    // Time Complexity: O(M), Space Complexity: O(M), where M is the length of the word.
    void insert(string words) {
        Node* curr = root;

        for (int i = 0; i < words.size(); i++) {
            int idx = words[i] - 'a';  // Fixed indexing syntax for C++
            if (curr->children[idx] == NULL) {
                curr->children[idx] = new Node(words[i]);
            }
            curr = curr->children[idx];
        }
        curr->isEnd = true;  // Mark the end of the word
    }

    // Time Complexity: O(M), Space Complexity: O(1), where M is the length of the word
    bool search(string str, int start, int end) {
        Node* curr = root;
        
        for (int i = start; i < end; i++) {
            int idx = str[i] - 'a';  // Replaced `.charAt(i)` with `str[i]`
            if (curr->children[idx] == NULL) {
                return false;  // Word is not present
            }
            curr = curr->children[idx];
        }

        return curr->isEnd == true;  // Return true if the word is found and marked as the end
    }

    // Time Complexity: O(N^2 * M), Space Complexity: O(N), where N is the length of the string `s` and M is the average length of the words in the dictionary.
    bool wordBreak(string s, vector<string>& wordDict) {
        Node* root = new Node();

        // Insert all words of the dict into a Trie
        for (string word : wordDict) {
            insert(word);
        }

        int n = s.length();
        vector<bool> dp(n + 1, false);
        dp[0] = true;  // Empty string is always true

        // DP approach to check if word can be segmented
        for (int j = 1; j <= n; j++) {
            for (int i = 0; i < j; i++) {
                if (dp[i] && search(s, i, j)) {  // If valid word is found
                    dp[j] = true;
                    break;
                }
            }
        }

        return dp[n];  // Return if the entire string can be segmented
    }
};
25555