-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path3LengthPalindrome.java
More file actions
33 lines (27 loc) · 1.1 KB
/
3LengthPalindrome.java
File metadata and controls
33 lines (27 loc) · 1.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
class Solution {
public int countPalindromicSubsequence(String s) {
int [] minIndexExist = new int[26];
int [] maxIndexExist = new int[26];
for(int i = 0; i<26; i++){
minIndexExist[i] = Integer.MAX_VALUE;
maxIndexExist[i] = Integer.MIN_VALUE;
}
for(int i = 0; i<s.length(); i++){
int charIndex = s.charAt(i) - 'a';
minIndexExist[charIndex] = Math.min(minIndexExist[charIndex], i);
maxIndexExist[charIndex] = Math.max(maxIndexExist[charIndex], i);
}
int uniqueCount = 0;
for(int charIndex = 0; charIndex<26; charIndex++){
if(minIndexExist[charIndex] == Integer.MAX_VALUE || maxIndexExist[charIndex] == Integer.MIN_VALUE){
continue;
}
HashSet<Character> uniqueCharsBetween = new HashSet<>();
for(int j = minIndexExist[charIndex]+1; j<maxIndexExist[charIndex]; j++){
uniqueCharsBetween.add(s.charAt(j));
}
uniqueCount += uniqueCharsBetween.size();
}
return uniqueCount;
}
}