Skip to content

Commit 0bcecc7

Browse files
authored
Merge pull request #1995 from gnohgnij/1930-unique-3-length-palindromic-subsequences
Create: 1930-unique-3-length-palindromic-subsequences.java
2 parents 591af37 + 01afc14 commit 0bcecc7

File tree

1 file changed

+32
-0
lines changed

1 file changed

+32
-0
lines changed
+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
class Solution {
2+
public int countPalindromicSubsequence(String s) {
3+
char[] letters = new char[]{'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k',
4+
'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v',
5+
'w', 'x', 'y', 'z'};
6+
Set<Character> left = new HashSet<>();
7+
Map<Character, Integer> right = new HashMap<>();
8+
for(char c : s.toCharArray()) {
9+
right.put(c, right.getOrDefault(c, 0) + 1);
10+
}
11+
Set<String> res = new HashSet<>();
12+
13+
for(int mid = 0; mid < s.length(); mid++) {
14+
char c = s.charAt(mid);
15+
16+
right.put(c, right.get(c)-1);
17+
if(right.get(c) == 0) {
18+
right.remove(c);
19+
}
20+
21+
for(int i=0; i<26; i++) {
22+
if(left.contains(letters[i]) && right.containsKey(letters[i])) {
23+
res.add("" + letters[i] + c + letters[i]);
24+
}
25+
}
26+
27+
left.add(c);
28+
}
29+
30+
return res.size();
31+
}
32+
}

0 commit comments

Comments
 (0)