Skip to content

Latest commit

 

History

History
68 lines (55 loc) · 1.76 KB

File metadata and controls

68 lines (55 loc) · 1.76 KB

1461. Check If a String Contains All Binary Codes of Size K

Given a binary string s and an integer k.

Return true if every binary code of length k is a substring of s. Otherwise, return false.

Example 1:

Input: s = "00110110", k = 2
Output: true
Explanation: The binary codes of length 2 are "00", "01", "10" and "11". They can be all found as substrings at indicies 0, 1, 3 and 2 respectively.

Example 2:

Input: s = "00110", k = 2
Output: true

Example 3:

Input: s = "0110", k = 1
Output: true
Explanation: The binary codes of length 1 are "0" and "1", it is clear that both exist as a substring.

Example 4:

Input: s = "0110", k = 2
Output: false
Explanation: The binary code "00" is of length 2 and doesn't exist in the array.

Example 5:

Input: s = "0000000001011100", k = 4
Output: false

Constraints:

  • 1 <= s.length <= 5 * 105
  • s[i] is either '0' or '1'.
  • 1 <= k <= 20

Solutions (Rust)

1. Set

use std::collections::HashSet;

impl Solution {
    pub fn has_all_codes(s: String, k: i32) -> bool {
        if s.len() < k as usize {
            return false;
        }

        let mut x = i32::from_str_radix(&s[..k as usize], 2).unwrap();
        let y = 1 << k;
        let mut set = vec![x].into_iter().collect::<HashSet<_>>();

        for c in s.bytes().skip(k as usize) {
            x = (x << 1) % y + (c - b'0') as i32;
            set.insert(x);
        }

        set.len() == y as usize
    }
}