<p>Given a binary string <code>s</code> and a positive integer <code>n</code>, return <code>true</code><em> if the binary representation of all the integers in the range </em><code>[1, n]</code><em> are <strong>substrings</strong> of </em><code>s</code><em>, or </em><code>false</code><em> otherwise</em>.</p>

<p>A <strong>substring</strong> is a contiguous sequence of characters within a string.</p>

<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>
<pre><strong>Input:</strong> s = "0110", n = 3
<strong>Output:</strong> true
</pre><p><strong class="example">Example 2:</strong></p>
<pre><strong>Input:</strong> s = "0110", n = 4
<strong>Output:</strong> false
</pre>
<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>

<ul>
	<li><code>1 &lt;= s.length &lt;= 1000</code></li>
	<li><code>s[i]</code> is either <code>&#39;0&#39;</code> or <code>&#39;1&#39;</code>.</li>
	<li><code>1 &lt;= n &lt;= 10<sup>9</sup></code></li>
</ul>

class Solution {
public:
    struct Node {
        int val;
        vector<Node*> children;
        bool isEnd;
        
        Node(int v) {
            val = v;
            isEnd = false;
            children.resize(2);
        }
    };
    
    Node* root;
    
    void addToTrie(string word) {
        cout << word << ' ';
        Node* cur = root;
        
        for (char ch: word) {
            int idx = ch - '0';
            if (cur->children[idx] != NULL) {
                cur = cur->children[idx];
            }
            else {
                Node* temp = new Node(idx);
                cur->children[idx] = temp;
                cur = cur->children[idx];
            }
        }
        
        cur->isEnd = true;
    }
    
    bool existsInTrie(vector<int> bits) {
        Node* cur = root;
        
        for (int val: bits) {
            if (cur->children[val] == NULL) {
                return false;
            }
            else {
                cur = cur->children[val];
            }
        }
        
        return cur->isEnd;
    }
    
    vector<int> convertToBits(int num) {
        vector<int> bits;
        while (num != 0) {
            int val = num % 2;
            num /= 2;
            bits.push_back(val);
        }
        
        reverse(bits.begin(), bits.end());
        return bits;
    }
    
    bool queryString(string s, int n) {
        root = new Node(-1);
        
        for (int i=0; i<s.size(); ++i) {
            string cur = "";
            for (int j=i; j<s.size(); ++j) {
                if (cur.size() == 0 && s[j] == '0') {
                    continue;
                }
                
                cur += s[j];
                
                if (cur.size() > 32) {
                    break;
                }
                
                addToTrie(cur);
            }
            
        }
        
        // cout << endl;
        
        for (int i=n; i>0; --i) {
            vector<int> bits = convertToBits(i);
            
            // cout << i << " : ";
            // for (int bit: bits) {
            //     cout << bit << ' ';
            // }
            // cout << endl;
            
            if (!existsInTrie(bits)) {
                return false;
            }
        }        
        
        return true;
    }
};
25800