Skip to content

Commit 7609fe9

Browse files
committed
feat: add solutions to lc problem: No.1930
No.1930.Unique Length-3 Palindromic Subsequences
1 parent 8ea0cfa commit 7609fe9

File tree

6 files changed

+71
-78
lines changed

6 files changed

+71
-78
lines changed

solution/1900-1999/1930.Unique Length-3 Palindromic Subsequences/README.md

+29-26
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,14 @@
6464

6565
<!-- 这里可写通用的实现逻辑 -->
6666

67+
**方法一:枚举两端字符 + 哈希表**
68+
69+
由于字符串中只包含小写字母,因此我们可以直接枚举所有的两端字符。对于每一对两端字符 $c$,我们找出它们在字符串中第一次和最后一次出现的位置 $l$ 和 $r$,如果 $r - l > 1$,说明找到了满足条件的回文序列,我们将 $[l+1,..r-1]$ 之间的字符去重后统计个数,即为以 $c$ 为两端字符的回文序列个数,加入答案中。
70+
71+
枚举结束后,即可得到答案。
72+
73+
时间复杂度 $O(n \times C)$,空间复杂度 $O(C)$,其中 $n$ 为字符串长度,而 $C$ 为字符集大小。本题中 $C = 26$。
74+
6775
<!-- tabs:start -->
6876

6977
### **Python3**
@@ -73,14 +81,12 @@
7381
```python
7482
class Solution:
7583
def countPalindromicSubsequence(self, s: str) -> int:
76-
res = 0
77-
for i in range(26):
78-
c = chr(ord('a') + i)
79-
if c in s:
80-
l, r = s.index(c), s.rindex(c)
81-
chars = {s[j] for j in range(l + 1, r)}
82-
res += len(chars)
83-
return res
84+
ans = 0
85+
for c in ascii_lowercase:
86+
l, r = s.find(c), s.rfind(c)
87+
if r - l > 1:
88+
ans += len(set(s[l + 1: r]))
89+
return ans
8490
```
8591

8692
### **Java**
@@ -90,16 +96,16 @@ class Solution:
9096
```java
9197
class Solution {
9298
public int countPalindromicSubsequence(String s) {
93-
int res = 0;
99+
int ans = 0;
94100
for (char c = 'a'; c <= 'z'; ++c) {
95101
int l = s.indexOf(c), r = s.lastIndexOf(c);
96-
Set<Character> chars = new HashSet<>();
102+
Set<Character> cs = new HashSet<>();
97103
for (int i = l + 1; i < r; ++i) {
98-
chars.add(s.charAt(i));
104+
cs.add(s.charAt(i));
99105
}
100-
res += chars.size();
106+
ans += cs.size();
101107
}
102-
return res;
108+
return ans;
103109
}
104110
}
105111
```
@@ -110,34 +116,31 @@ class Solution {
110116
class Solution {
111117
public:
112118
int countPalindromicSubsequence(string s) {
113-
int res = 0;
119+
int ans = 0;
114120
for (char c = 'a'; c <= 'z'; ++c) {
115121
int l = s.find_first_of(c), r = s.find_last_of(c);
116-
unordered_set<char> chars;
117-
for (int i = l + 1; i < r; ++i) {
118-
chars.insert(s[i]);
119-
}
120-
res += chars.size();
122+
unordered_set<char> cs;
123+
for (int i = l + 1; i < r; ++i) cs.insert(s[i]);
124+
ans += cs.size();
121125
}
122-
return res;
126+
return ans;
123127
}
124128
};
125129
```
126130
127131
### **Go**
128132
129133
```go
130-
func countPalindromicSubsequence(s string) int {
131-
res := 0
134+
func countPalindromicSubsequence(s string) (ans int) {
132135
for c := 'a'; c <= 'z'; c++ {
133136
l, r := strings.Index(s, string(c)), strings.LastIndex(s, string(c))
134-
chars := make(map[byte]bool)
137+
cs := map[byte]struct{}{}
135138
for i := l + 1; i < r; i++ {
136-
chars[s[i]] = true
139+
cs[s[i]] = struct{}{}
137140
}
138-
res += len(chars)
141+
ans += len(cs)
139142
}
140-
return res
143+
return
141144
}
142145
```
143146

solution/1900-1999/1930.Unique Length-3 Palindromic Subsequences/README_EN.md

+21-26
Original file line numberDiff line numberDiff line change
@@ -65,31 +65,29 @@
6565
```python
6666
class Solution:
6767
def countPalindromicSubsequence(self, s: str) -> int:
68-
res = 0
69-
for i in range(26):
70-
c = chr(ord('a') + i)
71-
if c in s:
72-
l, r = s.index(c), s.rindex(c)
73-
chars = {s[j] for j in range(l + 1, r)}
74-
res += len(chars)
75-
return res
68+
ans = 0
69+
for c in ascii_lowercase:
70+
l, r = s.find(c), s.rfind(c)
71+
if r - l > 1:
72+
ans += len(set(s[l + 1: r]))
73+
return ans
7674
```
7775

7876
### **Java**
7977

8078
```java
8179
class Solution {
8280
public int countPalindromicSubsequence(String s) {
83-
int res = 0;
81+
int ans = 0;
8482
for (char c = 'a'; c <= 'z'; ++c) {
8583
int l = s.indexOf(c), r = s.lastIndexOf(c);
86-
Set<Character> chars = new HashSet<>();
84+
Set<Character> cs = new HashSet<>();
8785
for (int i = l + 1; i < r; ++i) {
88-
chars.add(s.charAt(i));
86+
cs.add(s.charAt(i));
8987
}
90-
res += chars.size();
88+
ans += cs.size();
9189
}
92-
return res;
90+
return ans;
9391
}
9492
}
9593
```
@@ -100,34 +98,31 @@ class Solution {
10098
class Solution {
10199
public:
102100
int countPalindromicSubsequence(string s) {
103-
int res = 0;
101+
int ans = 0;
104102
for (char c = 'a'; c <= 'z'; ++c) {
105103
int l = s.find_first_of(c), r = s.find_last_of(c);
106-
unordered_set<char> chars;
107-
for (int i = l + 1; i < r; ++i) {
108-
chars.insert(s[i]);
109-
}
110-
res += chars.size();
104+
unordered_set<char> cs;
105+
for (int i = l + 1; i < r; ++i) cs.insert(s[i]);
106+
ans += cs.size();
111107
}
112-
return res;
108+
return ans;
113109
}
114110
};
115111
```
116112
117113
### **Go**
118114
119115
```go
120-
func countPalindromicSubsequence(s string) int {
121-
res := 0
116+
func countPalindromicSubsequence(s string) (ans int) {
122117
for c := 'a'; c <= 'z'; c++ {
123118
l, r := strings.Index(s, string(c)), strings.LastIndex(s, string(c))
124-
chars := make(map[byte]bool)
119+
cs := map[byte]struct{}{}
125120
for i := l + 1; i < r; i++ {
126-
chars[s[i]] = true
121+
cs[s[i]] = struct{}{}
127122
}
128-
res += len(chars)
123+
ans += len(cs)
129124
}
130-
return res
125+
return
131126
}
132127
```
133128

Original file line numberDiff line numberDiff line change
@@ -1,15 +1,13 @@
11
class Solution {
22
public:
33
int countPalindromicSubsequence(string s) {
4-
int res = 0;
4+
int ans = 0;
55
for (char c = 'a'; c <= 'z'; ++c) {
66
int l = s.find_first_of(c), r = s.find_last_of(c);
7-
unordered_set<char> chars;
8-
for (int i = l + 1; i < r; ++i) {
9-
chars.insert(s[i]);
10-
}
11-
res += chars.size();
7+
unordered_set<char> cs;
8+
for (int i = l + 1; i < r; ++i) cs.insert(s[i]);
9+
ans += cs.size();
1210
}
13-
return res;
11+
return ans;
1412
}
1513
};
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,11 @@
1-
func countPalindromicSubsequence(s string) int {
2-
res := 0
1+
func countPalindromicSubsequence(s string) (ans int) {
32
for c := 'a'; c <= 'z'; c++ {
43
l, r := strings.Index(s, string(c)), strings.LastIndex(s, string(c))
5-
chars := make(map[byte]bool)
4+
cs := map[byte]struct{}{}
65
for i := l + 1; i < r; i++ {
7-
chars[s[i]] = true
6+
cs[s[i]] = struct{}{}
87
}
9-
res += len(chars)
8+
ans += len(cs)
109
}
11-
return res
10+
return
1211
}
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
class Solution {
22
public int countPalindromicSubsequence(String s) {
3-
int res = 0;
3+
int ans = 0;
44
for (char c = 'a'; c <= 'z'; ++c) {
55
int l = s.indexOf(c), r = s.lastIndexOf(c);
6-
Set<Character> chars = new HashSet<>();
6+
Set<Character> cs = new HashSet<>();
77
for (int i = l + 1; i < r; ++i) {
8-
chars.add(s.charAt(i));
8+
cs.add(s.charAt(i));
99
}
10-
res += chars.size();
10+
ans += cs.size();
1111
}
12-
return res;
12+
return ans;
1313
}
1414
}
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,8 @@
11
class Solution:
22
def countPalindromicSubsequence(self, s: str) -> int:
3-
res = 0
4-
for i in range(26):
5-
c = chr(ord('a') + i)
6-
if c in s:
7-
l, r = s.index(c), s.rindex(c)
8-
chars = {s[j] for j in range(l + 1, r)}
9-
res += len(chars)
10-
return res
3+
ans = 0
4+
for c in ascii_lowercase:
5+
l, r = s.find(c), s.rfind(c)
6+
if r - l > 1:
7+
ans += len(set(s[l + 1: r]))
8+
return ans

0 commit comments

Comments
 (0)