<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:
    
    int convert(std::string const & a) {
        int ans = 0;
        
        int x = 0;
        for(int i = a.size() -1 ; i >= 0; i--) {
            if(a[i] == '1') ans += (1<<x);
            x++;
        }
        
        return ans;
    }
    
    bool queryString(string s, int n) {
        if(n >= 1048576) return false;
        
        if(n == 1 and s.size() == 1) {
            return s[0] == '1';
        }
        
        std::vector<int> sorted;
        for(int i = 0 ; i < s.size(); ++i) {
            for(int j = i ; j < s.size(); ++j) {
                if(j -i +1 >= 31) continue;
                //auto sub = s.substr(i, j-i +1);
               // if(sub.size())
                //std::cout << sub << " " << convert(sub) << std::endl;
                auto converted_val = convert(s.substr(i, j-i+1));
                //std::cout << converted_val << std::endl;
                if(converted_val <= n)
                    sorted.push_back(converted_val);
            }
        }
        
        
        //std::cout << sorted.size() << std::endl;
        std::sort(sorted.begin(), sorted.end());
        
        //for(int i = 0 ; i < sorted.size(); ++i) std::cout << sorted[i] << std::endl;
        
        auto found = true;
        auto looking_for = 1;
        for(int i = 0 ; i < sorted.size(); ++i) {
            if(sorted[i] == 0) continue;
            if(sorted[i] == looking_for) {
                looking_for += 1;
            }
        }
        
        return looking_for > n;
    }
};
15700