leetcode 1023. Binary String With Substrings Representing 1 To N

S has a maximum length of 1000, and it's easy to figure out the total count of numbers combined by substring of S.

Which is

1 + min(2, S) + min(2^2, S) + … + min(2^31, S), which is less than 1000 * 31

So within 1000 * 31 iterate from N to 1, result can be judged.

We can just use a brute-force to deal with the problem.

    class Solution {
        public boolean queryString(String S, int N) {
            for (int i = N; i >= 1; --i) {
                if (!S.contains(Integer.toBinaryString(i))) {
                    return false;
                }
            }
            return true;
        }
    }
posted on 2019-03-25 21:51  王 帅  阅读(70)  评论(0编辑  收藏  举报