Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit df14a92

Browse files
committed
feat: add solutions to lc problem: No.1638
No.1638.Count Substrings That Differ by One Character
1 parent 178ca64 commit df14a92

File tree

6 files changed

+246
-2
lines changed

6 files changed

+246
-2
lines changed

‎solution/1600-1699/1638.Count Substrings That Differ by One Character/README.md‎

Lines changed: 89 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,22 +70,110 @@
7070

7171
<!-- 这里可写通用的实现逻辑 -->
7272

73+
**方法一:枚举**
74+
75+
枚举不同的那个字符,然后向两边扩展。
76+
77+
时间复杂度 $O(m \times n \times min(m, n))$ 。
78+
7379
<!-- tabs:start -->
7480

7581
### **Python3**
7682

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

7985
```python
80-
86+
class Solution:
87+
def countSubstrings(self, s: str, t: str) -> int:
88+
m, n = len(s), len(t)
89+
ans = 0
90+
for i in range(m):
91+
for j in range(n):
92+
if s[i] != t[j]:
93+
l = r = 1
94+
while i - l >= 0 and j - l >= 0 and s[i - l] == t[j - l]:
95+
l += 1
96+
while i + r < m and j + r < n and s[i + r] == t[j + r]:
97+
r += 1
98+
ans += l * r
99+
return ans
81100
```
82101

83102
### **Java**
84103

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

87106
```java
107+
class Solution {
108+
public int countSubstrings(String s, String t) {
109+
int m = s.length(), n = t.length();
110+
int ans = 0;
111+
for (int i = 0; i < m; ++i) {
112+
for (int j = 0; j < n; ++j) {
113+
if (s.charAt(i) != t.charAt(j)) {
114+
int l = 1, r = 1;
115+
while (i - l >= 0 && j - l >= 0 && s.charAt(i - l) == t.charAt(j - l)) {
116+
++l;
117+
}
118+
while (i + r < m && j + r < n && s.charAt(i + r) == t.charAt(j + r)) {
119+
++r;
120+
}
121+
ans += l * r;
122+
}
123+
}
124+
}
125+
return ans;
126+
}
127+
}
128+
```
129+
130+
### **C++**
131+
132+
```cpp
133+
class Solution {
134+
public:
135+
int countSubstrings(string s, string t) {
136+
int m = s.size(), n = t.size();
137+
int ans = 0;
138+
for (int i = 0; i < m; ++i)
139+
{
140+
for (int j = 0; j < n; ++j)
141+
{
142+
if (s[i] == t[j]) continue;
143+
int l = 1, r = 1;
144+
while (i - l >= 0 && j - l >= 0 && s[i - l] == t[j - l]) ++l;
145+
while (i + r < m && j + r < n && s[i + r] == t[j + r]) ++r;
146+
ans += l * r;
147+
}
148+
}
149+
return ans;
150+
}
151+
};
152+
```
88153
154+
### **Go**
155+
156+
```go
157+
func countSubstrings(s string, t string) int {
158+
m, n := len(s), len(t)
159+
ans := 0
160+
for i := range s {
161+
for j := range t {
162+
if s[i] == t[j] {
163+
continue
164+
}
165+
l, r := 1, 1
166+
for i-l >= 0 && j-l >= 0 && s[i-l] == t[j-l] {
167+
l++
168+
}
169+
for i+r < m && j+r < n && s[i+r] == t[j+r] {
170+
r++
171+
}
172+
ans += l * r
173+
}
174+
}
175+
return ans
176+
}
89177
```
90178

91179
### **...**

‎solution/1600-1699/1638.Count Substrings That Differ by One Character/README_EN.md‎

Lines changed: 83 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,13 +55,95 @@ The underlined portions are the substrings that are chosen from s and t.
5555
### **Python3**
5656

5757
```python
58-
58+
class Solution:
59+
def countSubstrings(self, s: str, t: str) -> int:
60+
m, n = len(s), len(t)
61+
ans = 0
62+
for i in range(m):
63+
for j in range(n):
64+
if s[i] != t[j]:
65+
l = r = 1
66+
while i - l >= 0 and j - l >= 0 and s[i - l] == t[j - l]:
67+
l += 1
68+
while i + r < m and j + r < n and s[i + r] == t[j + r]:
69+
r += 1
70+
ans += l * r
71+
return ans
5972
```
6073

6174
### **Java**
6275

6376
```java
77+
class Solution {
78+
public int countSubstrings(String s, String t) {
79+
int m = s.length(), n = t.length();
80+
int ans = 0;
81+
for (int i = 0; i < m; ++i) {
82+
for (int j = 0; j < n; ++j) {
83+
if (s.charAt(i) != t.charAt(j)) {
84+
int l = 1, r = 1;
85+
while (i - l >= 0 && j - l >= 0 && s.charAt(i - l) == t.charAt(j - l)) {
86+
++l;
87+
}
88+
while (i + r < m && j + r < n && s.charAt(i + r) == t.charAt(j + r)) {
89+
++r;
90+
}
91+
ans += l * r;
92+
}
93+
}
94+
}
95+
return ans;
96+
}
97+
}
98+
```
99+
100+
### **C++**
101+
102+
```cpp
103+
class Solution {
104+
public:
105+
int countSubstrings(string s, string t) {
106+
int m = s.size(), n = t.size();
107+
int ans = 0;
108+
for (int i = 0; i < m; ++i)
109+
{
110+
for (int j = 0; j < n; ++j)
111+
{
112+
if (s[i] == t[j]) continue;
113+
int l = 1, r = 1;
114+
while (i - l >= 0 && j - l >= 0 && s[i - l] == t[j - l]) ++l;
115+
while (i + r < m && j + r < n && s[i + r] == t[j + r]) ++r;
116+
ans += l * r;
117+
}
118+
}
119+
return ans;
120+
}
121+
};
122+
```
64123
124+
### **Go**
125+
126+
```go
127+
func countSubstrings(s string, t string) int {
128+
m, n := len(s), len(t)
129+
ans := 0
130+
for i := range s {
131+
for j := range t {
132+
if s[i] == t[j] {
133+
continue
134+
}
135+
l, r := 1, 1
136+
for i-l >= 0 && j-l >= 0 && s[i-l] == t[j-l] {
137+
l++
138+
}
139+
for i+r < m && j+r < n && s[i+r] == t[j+r] {
140+
r++
141+
}
142+
ans += l * r
143+
}
144+
}
145+
return ans
146+
}
65147
```
66148

67149
### **...**
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
class Solution {
2+
public:
3+
int countSubstrings(string s, string t) {
4+
int m = s.size(), n = t.size();
5+
int ans = 0;
6+
for (int i = 0; i < m; ++i)
7+
{
8+
for (int j = 0; j < n; ++j)
9+
{
10+
if (s[i] == t[j]) continue;
11+
int l = 1, r = 1;
12+
while (i - l >= 0 && j - l >= 0 && s[i - l] == t[j - l]) ++l;
13+
while (i + r < m && j + r < n && s[i + r] == t[j + r]) ++r;
14+
ans += l * r;
15+
}
16+
}
17+
return ans;
18+
}
19+
};
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
func countSubstrings(s string, t string) int {
2+
m, n := len(s), len(t)
3+
ans := 0
4+
for i := range s {
5+
for j := range t {
6+
if s[i] == t[j] {
7+
continue
8+
}
9+
l, r := 1, 1
10+
for i-l >= 0 && j-l >= 0 && s[i-l] == t[j-l] {
11+
l++
12+
}
13+
for i+r < m && j+r < n && s[i+r] == t[j+r] {
14+
r++
15+
}
16+
ans += l * r
17+
}
18+
}
19+
return ans
20+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
class Solution {
2+
public int countSubstrings(String s, String t) {
3+
int m = s.length(), n = t.length();
4+
int ans = 0;
5+
for (int i = 0; i < m; ++i) {
6+
for (int j = 0; j < n; ++j) {
7+
if (s.charAt(i) != t.charAt(j)) {
8+
int l = 1, r = 1;
9+
while (i - l >= 0 && j - l >= 0 && s.charAt(i - l) == t.charAt(j - l)) {
10+
++l;
11+
}
12+
while (i + r < m && j + r < n && s.charAt(i + r) == t.charAt(j + r)) {
13+
++r;
14+
}
15+
ans += l * r;
16+
}
17+
}
18+
}
19+
return ans;
20+
}
21+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
class Solution:
2+
def countSubstrings(self, s: str, t: str) -> int:
3+
m, n = len(s), len(t)
4+
ans = 0
5+
for i in range(m):
6+
for j in range(n):
7+
if s[i] != t[j]:
8+
l = r = 1
9+
while i - l >= 0 and j - l >= 0 and s[i - l] == t[j - l]:
10+
l += 1
11+
while i + r < m and j + r < n and s[i + r] == t[j + r]:
12+
r += 1
13+
ans += l * r
14+
return ans

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /