Skip to content

Commit f81e8d4

Browse files
committed
feat: add solutions to lcof2 problem: No.020
剑指 Offer II 020. 回文子字符串的个数
1 parent bcb8472 commit f81e8d4

File tree

3 files changed

+125
-1
lines changed

3 files changed

+125
-1
lines changed

lcof2/剑指 Offer II 020. 回文子字符串的个数/README.md

Lines changed: 93 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,14 +42,40 @@
4242

4343
<!-- 这里可写通用的实现逻辑 -->
4444

45-
在 Manacher 算法的计算过程中,`p[i] - 1` 表示以第 `i` 位为中心的最大回文长度,`(p[i] - 1) / 2` **向上取整**即可得到以第 `i` 位为中心的回文串数量
45+
**方法一:从中心向两侧扩展回文串**
46+
47+
我们可以枚举回文串的中间点,然后向左右两边扩展,统计回文串的数量。注意,回文串可能包含奇数个字符,也可能不包含。因此这两种情况都要考虑。
48+
49+
时间复杂度 $O(n^2)$,其中 $n$ 是字符串 `s` 的长度。
50+
51+
**方法二:Manacher 算法**
52+
53+
在 Manacher 算法的计算过程中,用 $p[i]-1$ 表示以第 $i$ 位为中心的最大回文长度,以第 $i$ 位为中心的回文串数量为 $\left \lceil \frac{p[i]-1}{2} \right \rceil$。
54+
55+
时间复杂度 $O(n)$,空间复杂度 $O(n)$。其中 $n$ 是字符串 `s` 的长度。
4656

4757
<!-- tabs:start -->
4858

4959
### **Python3**
5060

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

63+
```python
64+
class Solution:
65+
def countSubstrings(self, s: str) -> int:
66+
def f(i, j):
67+
cnt = 0
68+
while i >= 0 and j < n:
69+
if s[i] != s[j]:
70+
break
71+
cnt += 1
72+
i, j = i - 1, j + 1
73+
return cnt
74+
75+
n = len(s)
76+
return sum(f(i, i) + f(i, i + 1) for i in range(n))
77+
```
78+
5379
```python
5480
class Solution:
5581
def countSubstrings(self, s: str) -> int:
@@ -73,6 +99,30 @@ class Solution:
7399

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

102+
```java
103+
class Solution {
104+
private String s;
105+
106+
public int countSubstrings(String s) {
107+
int ans = 0;
108+
this.s = s;
109+
for (int i = 0; i < s.length(); ++i) {
110+
ans += f(i, i);
111+
ans += f(i, i + 1);
112+
}
113+
return ans;
114+
}
115+
116+
private int f(int i, int j) {
117+
int cnt = 0;
118+
for (; i >= 0 && j < s.length() && s.charAt(i) == s.charAt(j); --i, ++j) {
119+
++cnt;
120+
}
121+
return cnt;
122+
}
123+
}
124+
```
125+
76126
```java
77127
class Solution {
78128
public int countSubstrings(String s) {
@@ -101,6 +151,48 @@ class Solution {
101151
}
102152
```
103153

154+
### **C++**
155+
156+
```cpp
157+
class Solution {
158+
public:
159+
int countSubstrings(string s) {
160+
int ans = 0;
161+
int n = s.size();
162+
auto f = [&](int i, int j) -> int {
163+
int cnt = 0;
164+
for (; i >= 0 && j < n && s[i] == s[j]; --i, ++j) {
165+
++cnt;
166+
}
167+
return cnt;
168+
};
169+
for (int i = 0; i < n; ++i) {
170+
ans += f(i, i) + f(i, i + 1);
171+
}
172+
return ans;
173+
}
174+
};
175+
```
176+
177+
### **Go**
178+
179+
```go
180+
func countSubstrings(s string) (ans int) {
181+
n := len(s)
182+
f := func(i, j int) (cnt int) {
183+
for ; i >= 0 && j < n && s[i] == s[j]; i, j = i-1, j+1 {
184+
cnt++
185+
}
186+
return
187+
}
188+
for i := range s {
189+
ans += f(i, i)
190+
ans += f(i, i+1)
191+
}
192+
return
193+
}
194+
```
195+
104196
### **...**
105197

106198
```
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
class Solution {
2+
public:
3+
int countSubstrings(string s) {
4+
int ans = 0;
5+
int n = s.size();
6+
auto f = [&](int i, int j) -> int {
7+
int cnt = 0;
8+
for (; i >= 0 && j < n && s[i] == s[j]; --i, ++j) {
9+
++cnt;
10+
}
11+
return cnt;
12+
};
13+
for (int i = 0; i < n; ++i) {
14+
ans += f(i, i) + f(i, i + 1);
15+
}
16+
return ans;
17+
}
18+
};
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
func countSubstrings(s string) (ans int) {
2+
n := len(s)
3+
f := func(i, j int) (cnt int) {
4+
for ; i >= 0 && j < n && s[i] == s[j]; i, j = i-1, j+1 {
5+
cnt++
6+
}
7+
return
8+
}
9+
for i := range s {
10+
ans += f(i, i)
11+
ans += f(i, i+1)
12+
}
13+
return
14+
}

0 commit comments

Comments
 (0)