Skip to content

Commit ed4e3f0

Browse files
chore: add LeetCode daily solution
1 parent a2eae88 commit ed4e3f0

File tree

5 files changed

+92
-0
lines changed

5 files changed

+92
-0
lines changed
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
# Unique Length-3 Palindromic Subsequences (Medium)
2+
3+
**Problem ID:** 1930
4+
**Date:** 2025-11-21
5+
**Link:** https://leetcode.com/problems/unique-length-3-palindromic-subsequences/
6+
7+
## Approach
8+
9+
To solve the problem of counting unique length-3 palindromic subsequences in a string, we can adopt the following approach:
10+
11+
### Main Idea:
12+
A palindromic subsequence of length 3 has the form "aba", where 'a' is the first and last character, and 'b' is the middle character. To find all unique palindromic subsequences of this form, we need to identify pairs of characters that can serve as 'a' and 'b' in the string.
13+
14+
### Approach:
15+
1. **Character Index Tracking**: Use a dictionary to track the indices of each character in the string. This will help us quickly find potential positions for the characters 'a' and 'b'.
16+
17+
2. **Iterate Over Characters**: For each character 'b' in the string (considering it as the middle character of the palindrome):
18+
- Check for all characters 'a' that appear before 'b' (using the index list from the dictionary).
19+
- Check for all characters 'a' that appear after 'b'.
20+
- For each valid pair of indices (one before and one after), we can form the palindrome "aba".
21+
22+
3. **Set for Uniqueness**: Use a set to store the unique palindromic subsequences we find. This ensures that duplicates are automatically handled.
23+
24+
### Data Structures:
25+
- A dictionary (or hashmap) to store the indices of each character.
26+
- A set to store unique palindromic subsequences.
27+
28+
### Complexity:
29+
- **Time Complexity**: O(n^2) in the worst case, where n is the length of the string. This is due to the nested iteration over characters and their indices. However, since we are only considering pairs of indices for each middle character, the average case can be better.
30+
- **Space Complexity**: O(n) for storing the indices of characters in the dictionary and for the set of unique palindromic subsequences.
31+
32+
### Conclusion:
33+
By systematically identifying potential palindromic structures and leveraging data structures for efficient lookups, we can effectively count the unique length-3 palindromic subsequences in the string. This method balances clarity and efficiency, making it suitable for the problem's constraints.
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
import java.util.HashSet;
2+
3+
public class Solution {
4+
public int countPalindromicSubseq(String s) {
5+
HashSet<String> uniquePalindromes = new HashSet<>();
6+
int n = s.length();
7+
8+
for (int i = 0; i < n; i++) {
9+
for (int j = i + 1; j < n; j++) {
10+
if (s.charAt(i) == s.charAt(j)) {
11+
for (int k = j + 1; k < n; k++) {
12+
if (s.charAt(i) == s.charAt(k)) {
13+
uniquePalindromes.add("" + s.charAt(i) + s.charAt(j) + s.charAt(k));
14+
}
15+
}
16+
}
17+
}
18+
}
19+
20+
return uniquePalindromes.size();
21+
}
22+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
var countPalindromicSubsequences = function(s) {
2+
const seen = new Set();
3+
const n = s.length;
4+
5+
for (let i = 0; i < n; i++) {
6+
for (let j = i + 1; j < n; j++) {
7+
if (s[i] === s[j]) {
8+
for (let k = 0; k < n; k++) {
9+
if (k !== i && k !== j && s[k] === s[i]) {
10+
seen.add(s[i] + s[j] + s[i]);
11+
}
12+
}
13+
}
14+
}
15+
}
16+
17+
return seen.size;
18+
};
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
class Solution:
2+
def uniquePalindromicSubseq(self, s: str) -> int:
3+
seen = set()
4+
last = {}
5+
for i, char in enumerate(s):
6+
if char in last:
7+
for j in last[char]:
8+
seen.add(char + s[j] + char)
9+
if char not in last:
10+
last[char] = []
11+
last[char].append(i)
12+
return len(seen)
13+
14+
# Example test cases
15+
sol = Solution()
16+
print(sol.uniquePalindromicSubseq("aabca")) # Output: 3
17+
print(sol.uniquePalindromicSubseq("adc")) # Output: 0
18+
print(sol.uniquePalindromicSubseq("bbcbaba")) # Output: 4

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -285,3 +285,4 @@ Through completing the Blind 75 and NeetCode 150, you will have mastered:
285285
- 2025-11-18 — [1-bit and 2-bit Characters](https://leetcode.com/problems/1-bit-and-2-bit-characters/) (Easy) → `Easy/2025-11-18-717-1-bit-and-2-bit-Characters`
286286
- 2025-11-19 — [Keep Multiplying Found Values by Two](https://leetcode.com/problems/keep-multiplying-found-values-by-two/) (Easy) → `Easy/2025-11-19-2154-Keep-Multiplying-Found-Values-by-Two`
287287
- 2025-11-20 — [Set Intersection Size At Least Two](https://leetcode.com/problems/set-intersection-size-at-least-two/) (Hard) → `Hard/2025-11-20-757-Set-Intersection-Size-At-Least-Two`
288+
- 2025-11-21 — [Unique Length-3 Palindromic Subsequences](https://leetcode.com/problems/unique-length-3-palindromic-subsequences/) (Medium) → `Medium/2025-11-21-1930-Unique-Length-3-Palindromic-Subsequences`

0 commit comments

Comments
 (0)