[LeetCode] Number of Distinct Binary Strings After Applying Operations

2450. Number of Distinct Binary Strings After Applying Operations

You are given a binary string s and a positive integer k.

You can apply the following operation on the string any number of times:

  • Choose any substring of size k from s and flip all its characters, that is, turn all 1’s into 0’s, and all 0’s into 1’s.

Return the number of distinct strings you can obtain. Since the answer may be too large, return it modulo 109 + 7.

Note that:

  • A binary string is a string that consists only of the characters 0 and 1.
  • A substring is a contiguous part of a string.
1
2
3
4
5
6
7
8
9
class Solution {
public:
int countDistinctStrings(string s, int k) {
if(s.length() == k) return 2;
long long res = 1, mod = 1e9 + 7;
for(int i = 0; i <= s.length() - k; i++) res = res * 2 % mod;
return res;
}
};
Author: Song Hayoung
Link: https://songhayoung.github.io/2023/01/31/PS/LeetCode/number-of-distinct-binary-strings-after-applying-operations/
Copyright Notice: All articles in this blog are licensed under CC BY-NC-SA 4.0 unless stating additionally.