1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
|
fn main() {
println!("Hello, world!");
let tests = [
("00110110", 2, true),
("0110", 1, true),
("0110", 2, false),
("00000000001011100", 3, true),
("000011010111011001001111111001000100100100010100101100001101101101110001100100101111100111001001111001001010111010010101101001001110011100110101001001001000000110101001010011101100110110100010000", 7, false)
];
for (s, k, o) in tests {
let result = Solution::has_all_codes(s.to_string(),k);
println!("{s:?} with {k} is {result} should be {o}");
}
}
struct Solution;
impl Solution {
pub fn has_all_codes(s: String, k: i32) -> bool {
// (00..=2_i32.pow(k as u32) -1).all(|x| s.contains(&format!("{x:00$b}", k as usize)))j
let mut m: u128 = 0;
let mut m = (0..)
for w in s.chars().collect::<Vec<_>>().windows(k as usize) {
m |= 1 << i32::from_str_radix(&w.iter().collect::<String>(), 2).unwrap();
// println!("{m:b}");
}
// println!("{m:b}");
let h = 2_u128.pow(2_u32.pow(k as u32)) -1;
// println!("{h} {h:b}");
m == h
}
}
|