返回介绍

solution / 1400-1499 / 1461.Check If a String Contains All Binary Codes of Size K / README_EN

发布于 2024-06-17 01:03:19 字数 5253 浏览 0 评论 0 收藏 0

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

中文文档

Description

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 indices 0, 1, 3 and 2 respectively.

Example 2:

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 3:

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

 

Constraints:

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

Solutions

Solution 1

class Solution:
  def hasAllCodes(self, s: str, k: int) -> bool:
    ss = {s[i : i + k] for i in range(len(s) - k + 1)}
    return len(ss) == 1 << k
class Solution {
  public boolean hasAllCodes(String s, int k) {
    Set<String> ss = new HashSet<>();
    for (int i = 0; i < s.length() - k + 1; ++i) {
      ss.add(s.substring(i, i + k));
    }
    return ss.size() == 1 << k;
  }
}
class Solution {
public:
  bool hasAllCodes(string s, int k) {
    unordered_set<string> ss;
    for (int i = 0; i + k <= s.size(); ++i) {
      ss.insert(move(s.substr(i, k)));
    }
    return ss.size() == 1 << k;
  }
};
func hasAllCodes(s string, k int) bool {
  ss := map[string]bool{}
  for i := 0; i+k <= len(s); i++ {
    ss[s[i:i+k]] = true
  }
  return len(ss) == 1<<k
}

Solution 2

class Solution:
  def hasAllCodes(self, s: str, k: int) -> bool:
    if len(s) - k + 1 < (1 << k):
      return False
    vis = [False] * (1 << k)
    num = int(s[:k], 2)
    vis[num] = True
    for i in range(k, len(s)):
      a = (ord(s[i - k]) - ord('0')) << (k - 1)
      b = ord(s[i]) - ord('0')
      num = ((num - a) << 1) + b
      vis[num] = True
    return all(v for v in vis)
class Solution {
  public boolean hasAllCodes(String s, int k) {
    int n = s.length();
    if (n - k + 1 < (1 << k)) {
      return false;
    }
    boolean[] vis = new boolean[1 << k];
    int num = Integer.parseInt(s.substring(0, k), 2);
    vis[num] = true;
    for (int i = k; i < n; ++i) {
      int a = (s.charAt(i - k) - '0') << (k - 1);
      int b = s.charAt(i) - '0';
      num = (num - a) << 1 | b;
      vis[num] = true;
    }
    for (boolean v : vis) {
      if (!v) {
        return false;
      }
    }
    return true;
  }
}
class Solution {
public:
  bool hasAllCodes(string s, int k) {
    int n = s.size();
    if (n - k + 1 < (1 << k)) return false;
    vector<bool> vis(1 << k);
    int num = stoi(s.substr(0, k), nullptr, 2);
    vis[num] = true;
    for (int i = k; i < n; ++i) {
      int a = (s[i - k] - '0') << (k - 1);
      int b = s[i] - '0';
      num = (num - a) << 1 | b;
      vis[num] = true;
    }
    for (bool v : vis)
      if (!v) return false;
    return true;
  }
};
func hasAllCodes(s string, k int) bool {
  n := len(s)
  if n-k+1 < (1 << k) {
    return false
  }
  vis := make([]bool, 1<<k)
  num := 0
  for i := 0; i < k; i++ {
    num = num<<1 | int(s[i]-'0')
  }
  vis[num] = true
  for i := k; i < n; i++ {
    a := int(s[i-k]-'0') << (k - 1)
    num = (num-a)<<1 | int(s[i]-'0')
    vis[num] = true
  }
  for _, v := range vis {
    if !v {
      return false
    }
  }
  return true
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文