summaryrefslogtreecommitdiff
path: root/check-if-a-string-contains-all-binary-codes-of-size-k/src/main.rs
diff options
context:
space:
mode:
authorOrangerot <purple@orangerot.dev>2024-06-27 11:30:16 +0200
committerOrangerot <purple@orangerot.dev>2024-06-27 11:30:16 +0200
commit4b0a6a01b051a4ebfbc17661d14cb23fe4f275fb (patch)
tree0072cca328fe5adb2ed61004010228ff85e2164d /check-if-a-string-contains-all-binary-codes-of-size-k/src/main.rs
Initial commitHEADmain
Diffstat (limited to 'check-if-a-string-contains-all-binary-codes-of-size-k/src/main.rs')
-rw-r--r--check-if-a-string-contains-all-binary-codes-of-size-k/src/main.rs32
1 files changed, 32 insertions, 0 deletions
diff --git a/check-if-a-string-contains-all-binary-codes-of-size-k/src/main.rs b/check-if-a-string-contains-all-binary-codes-of-size-k/src/main.rs
new file mode 100644
index 0000000..028b32c
--- /dev/null
+++ b/check-if-a-string-contains-all-binary-codes-of-size-k/src/main.rs
@@ -0,0 +1,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
+ }
+}