Skip to content

Commit

Permalink
Time: 191 ms (57.59%), Space: 55.2 MB (5.5%) - LeetHub
Browse files Browse the repository at this point in the history
  • Loading branch information
Amit-S-Sahu committed Jan 4, 2025
1 parent 13b0822 commit 085fa90
Showing 1 changed file with 26 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
class Solution {
public int countPalindromicSubsequence(String s) {
HashMap<Character, ArrayList<Integer>> map = new HashMap<>();
for (int i = 0; i < s.length(); i++) {
map.putIfAbsent(s.charAt(i), new ArrayList<>());
map.get(s.charAt(i)).add(i);
}

int ans = 0;
for (HashMap.Entry<Character, ArrayList<Integer>> entry : map.entrySet()) {
ArrayList<Integer> indices = entry.getValue();
int start = indices.get(0);
int end = indices.get(indices.size() - 1);

if (end - start <= 1) continue;

HashSet<Character> seen = new HashSet<>();
for (int i = start + 1; i < end; i++) {
seen.add(s.charAt(i));
}
ans += seen.size();
}

return ans;
}
}

0 comments on commit 085fa90

Please sign in to comment.