返回介绍

solution / 1900-1999 / 1987.Number of Unique Good Subsequences / README_EN

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

1987. Number of Unique Good Subsequences

中文文档

Description

You are given a binary string binary. A subsequence of binary is considered good if it is not empty and has no leading zeros (with the exception of "0").

Find the number of unique good subsequences of binary.

  • For example, if binary = "001", then all the good subsequences are ["0", "0", "1"], so the unique good subsequences are "0" and "1". Note that subsequences "00", "01", and "001" are not good because they have leading zeros.

Return _the number of unique good subsequences of _binary. Since the answer may be very large, return it modulo 109 + 7.

A subsequence is a sequence that can be derived from another sequence by deleting some or no elements without changing the order of the remaining elements.

 

Example 1:

Input: binary = "001"
Output: 2
Explanation: The good subsequences of binary are ["0", "0", "1"].
The unique good subsequences are "0" and "1".

Example 2:

Input: binary = "11"
Output: 2
Explanation: The good subsequences of binary are ["1", "1", "11"].
The unique good subsequences are "1" and "11".

Example 3:

Input: binary = "101"
Output: 5
Explanation: The good subsequences of binary are ["1", "0", "1", "10", "11", "101"]. 
The unique good subsequences are "0", "1", "10", "11", and "101".

 

Constraints:

  • 1 <= binary.length <= 105
  • binary consists of only '0's and '1's.

Solutions

Solution 1

class Solution:
  def numberOfUniqueGoodSubsequences(self, binary: str) -> int:
    f = g = 0
    ans = 0
    mod = 10**9 + 7
    for c in binary:
      if c == "0":
        g = (g + f) % mod
        ans = 1
      else:
        f = (f + g + 1) % mod
    ans = (ans + f + g) % mod
    return ans
class Solution {
  public int numberOfUniqueGoodSubsequences(String binary) {
    final int mod = (int) 1e9 + 7;
    int f = 0, g = 0;
    int ans = 0;
    for (int i = 0; i < binary.length(); ++i) {
      if (binary.charAt(i) == '0') {
        g = (g + f) % mod;
        ans = 1;
      } else {
        f = (f + g + 1) % mod;
      }
    }
    ans = (ans + f + g) % mod;
    return ans;
  }
}
class Solution {
public:
  int numberOfUniqueGoodSubsequences(string binary) {
    const int mod = 1e9 + 7;
    int f = 0, g = 0;
    int ans = 0;
    for (char& c : binary) {
      if (c == '0') {
        g = (g + f) % mod;
        ans = 1;
      } else {
        f = (f + g + 1) % mod;
      }
    }
    ans = (ans + f + g) % mod;
    return ans;
  }
};
func numberOfUniqueGoodSubsequences(binary string) (ans int) {
  const mod int = 1e9 + 7
  f, g := 0, 0
  for _, c := range binary {
    if c == '0' {
      g = (g + f) % mod
      ans = 1
    } else {
      f = (f + g + 1) % mod
    }
  }
  ans = (ans + f + g) % mod
  return
}
function numberOfUniqueGoodSubsequences(binary: string): number {
  let [f, g] = [0, 0];
  let ans = 0;
  const mod = 1e9 + 7;
  for (const c of binary) {
    if (c === '0') {
      g = (g + f) % mod;
      ans = 1;
    } else {
      f = (f + g + 1) % mod;
    }
  }
  ans = (ans + f + g) % mod;
  return ans;
}

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

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

发布评论

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