Skip to content

Commit 689ae81

Browse files
committed
feat: add solutions to lc problem: No.2450
No.2450.Number of Distinct Binary Strings After Applying Operations
1 parent b8e514b commit 689ae81

File tree

6 files changed

+126
-2
lines changed

6 files changed

+126
-2
lines changed

solution/2400-2499/2450.Number of Distinct Binary Strings After Applying Operations/README.md

Lines changed: 49 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,22 +59,70 @@ It can be shown that we cannot obtain any other string, so the answer is 2.
5959

6060
<!-- 这里可写通用的实现逻辑 -->
6161

62+
**方法一:数学**
63+
64+
假设字符串 $s$ 长度为 $n$,那么长度为 $k$ 的子串共有 $n - k + 1$ 个,每个子串都可以翻转,因此共有 $2^{n - k + 1}$ 种翻转方式。
65+
66+
时间复杂度 $O(n)$,空间复杂度 $O(1)$。其中 $n$ 为字符串 $s$ 的长度。
67+
6268
<!-- tabs:start -->
6369

6470
### **Python3**
6571

6672
<!-- 这里可写当前语言的特殊实现逻辑 -->
6773

6874
```python
69-
75+
class Solution:
76+
def countDistinctStrings(self, s: str, k: int) -> int:
77+
return pow(2, len(s) - k + 1) % (10**9 + 7)
7078
```
7179

7280
### **Java**
7381

7482
<!-- 这里可写当前语言的特殊实现逻辑 -->
7583

7684
```java
85+
class Solution {
86+
public static final int MOD = (int) 1e9 + 7;
87+
88+
public int countDistinctStrings(String s, int k) {
89+
int ans = 1;
90+
for (int i = 0; i < s.length() - k + 1; ++i) {
91+
ans = (ans * 2) % MOD;
92+
}
93+
return ans;
94+
}
95+
}
96+
```
97+
98+
### **C++**
99+
100+
```cpp
101+
class Solution {
102+
public:
103+
const int mod = 1e9 + 7;
104+
105+
int countDistinctStrings(string s, int k) {
106+
int ans = 1;
107+
for (int i = 0; i < s.size() - k + 1; ++i) {
108+
ans = (ans * 2) % mod;
109+
}
110+
return ans;
111+
}
112+
};
113+
```
77114

115+
### **Go**
116+
117+
```go
118+
func countDistinctStrings(s string, k int) int {
119+
const mod int = 1e9 + 7
120+
ans := 1
121+
for i := 0; i < len(s)-k+1; i++ {
122+
ans = (ans * 2) % mod
123+
}
124+
return ans
125+
}
78126
```
79127

80128
### **TypeScript**

solution/2400-2499/2450.Number of Distinct Binary Strings After Applying Operations/README_EN.md

Lines changed: 43 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,13 +60,55 @@ It can be shown that we cannot obtain any other string, so the answer is 2.
6060
### **Python3**
6161

6262
```python
63-
63+
class Solution:
64+
def countDistinctStrings(self, s: str, k: int) -> int:
65+
return pow(2, len(s) - k + 1) % (10**9 + 7)
6466
```
6567

6668
### **Java**
6769

6870
```java
71+
class Solution {
72+
public static final int MOD = (int) 1e9 + 7;
73+
74+
public int countDistinctStrings(String s, int k) {
75+
int ans = 1;
76+
for (int i = 0; i < s.length() - k + 1; ++i) {
77+
ans = (ans * 2) % MOD;
78+
}
79+
return ans;
80+
}
81+
}
82+
```
83+
84+
### **C++**
85+
86+
```cpp
87+
class Solution {
88+
public:
89+
const int mod = 1e9 + 7;
90+
91+
int countDistinctStrings(string s, int k) {
92+
int ans = 1;
93+
for (int i = 0; i < s.size() - k + 1; ++i) {
94+
ans = (ans * 2) % mod;
95+
}
96+
return ans;
97+
}
98+
};
99+
```
69100

101+
### **Go**
102+
103+
```go
104+
func countDistinctStrings(s string, k int) int {
105+
const mod int = 1e9 + 7
106+
ans := 1
107+
for i := 0; i < len(s)-k+1; i++ {
108+
ans = (ans * 2) % mod
109+
}
110+
return ans
111+
}
70112
```
71113

72114
### **TypeScript**
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
class Solution {
2+
public:
3+
const int mod = 1e9 + 7;
4+
5+
int countDistinctStrings(string s, int k) {
6+
int ans = 1;
7+
for (int i = 0; i < s.size() - k + 1; ++i) {
8+
ans = (ans * 2) % mod;
9+
}
10+
return ans;
11+
}
12+
};
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
func countDistinctStrings(s string, k int) int {
2+
const mod int = 1e9 + 7
3+
ans := 1
4+
for i := 0; i < len(s)-k+1; i++ {
5+
ans = (ans * 2) % mod
6+
}
7+
return ans
8+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
class Solution {
2+
public static final int MOD = (int) 1e9 + 7;
3+
4+
public int countDistinctStrings(String s, int k) {
5+
int ans = 1;
6+
for (int i = 0; i < s.length() - k + 1; ++i) {
7+
ans = (ans * 2) % MOD;
8+
}
9+
return ans;
10+
}
11+
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
class Solution:
2+
def countDistinctStrings(self, s: str, k: int) -> int:
3+
return pow(2, len(s) - k + 1) % (10**9 + 7)

0 commit comments

Comments
 (0)