Skip to content

Commit

Permalink
Add Java solution to 1930 Unique 3 Length Palindromic Subsequences
Browse files Browse the repository at this point in the history
  • Loading branch information
gnohgnij committed Jan 11, 2023
1 parent afb94e9 commit 01afc14
Showing 1 changed file with 32 additions and 0 deletions.
32 changes: 32 additions & 0 deletions java/1930-unique-3-length-palindromic-subsequences.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
class Solution {
public int countPalindromicSubsequence(String s) {
char[] letters = new char[]{'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k',
'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v',
'w', 'x', 'y', 'z'};
Set<Character> left = new HashSet<>();
Map<Character, Integer> right = new HashMap<>();
for(char c : s.toCharArray()) {
right.put(c, right.getOrDefault(c, 0) + 1);
}
Set<String> res = new HashSet<>();

for(int mid = 0; mid < s.length(); mid++) {
char c = s.charAt(mid);

right.put(c, right.get(c)-1);
if(right.get(c) == 0) {
right.remove(c);
}

for(int i=0; i<26; i++) {
if(left.contains(letters[i]) && right.containsKey(letters[i])) {
res.add("" + letters[i] + c + letters[i]);
}
}

left.add(c);
}

return res.size();
}
}

0 comments on commit 01afc14

Please sign in to comment.