cs: add «1930. Unique Length-3 Palindromic Subsequences»

URL:	https://leetcode.com/problems/unique-length-3-palindromic-subsequences/
Signed-off-by: Matej Focko <me@mfocko.xyz>
This commit is contained in:
Matej Focko 2025-01-04 12:12:57 +01:00
parent 5c88e2bfe0
commit eb0e5a5391
Signed by: mfocko
SSH key fingerprint: SHA256:icm0fIOSJUpy5+1x23sfr+hLtF9UhY8VpMC7H4WFJP8

View file

@ -0,0 +1,31 @@
public class Solution {
private static readonly int NOT_FOUND = int.MaxValue;
public int CountPalindromicSubsequence(string s) {
var (first, last) = (new int[26], new int[26]);
Array.Fill(first, NOT_FOUND);
// Initialize the indices
for (var i = 0; i < s.Length; ++i) {
var idx = s[i] - 'a';
first[idx] = Math.Min(first[idx], i);
last[idx] = i;
}
var count = 0;
for (var c = 0; c < 26; ++c) {
if (first[c] == NOT_FOUND) {
continue;
}
var between = new HashSet<char>();
for (int i = first[c] + 1; i < last[c]; ++i) {
between.Add(s[i]);
}
count += between.Count;
}
return count;
}
}