LeetCode Solutions

1016. Binary String With Substrings Representing 1 To N

Time:

Space:

			

class Solution {
 public:
  bool queryString(string S, int N) {
    if (N > 1511)
      return false;

    for (int i = N; i > N / 2; --i) {
      string binary = bitset<32>(i).to_string();
      binary = binary.substr(binary.find("1"));
      if (S.find(binary) == string::npos)
        return false;
    }

    return true;
  }
};
			

class Solution {
  public boolean queryString(String S, int N) {
    if (N > 1511)
      return false;

    for (int i = N; i > N / 2; --i)
      if (!S.contains(Integer.toBinaryString(i)))
        return false;

    return true;
  }
}
			

class Solution:
  def queryString(self, S: str, N: int) -> bool:
    if N > 1511:
      return False

    for i in range(N, N // 2, -1):
      if format(i, 'b') not in S:
        return False

    return True