LeetCode 1016. Binary String With Substrings Representing 1 To N

Problem Statement

Typical BFS.


use std::collections::HashSet;

impl Solution {
pub fn query_string(s: String, n: i32) -> bool {
let mut set = HashSet::new();
for i in 0..s.len() {
for j in i..s.len() {
let str = String::from(&s[i..j + 1]);
let num = i32::from_str_radix(&str, 2).unwrap();
if num > n {
break;
}
set.insert(num);
}
}

for i in 1..n + 1 {
if !set.contains(&i) {
return false;
}
}
return true;
}
}