-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path4_Unique_Length-3_Palindromic_Subsequences.cpp
96 lines (76 loc) · 2.65 KB
/
4_Unique_Length-3_Palindromic_Subsequences.cpp
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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
// 1930. Unique Length-3 Palindromic Subsequences
// Given a string s, return the number of unique palindromes of length three that are a subsequence of s.
// Note that even if there are multiple ways to obtain the same subsequence, it is still only counted once.
// A palindrome is a string that reads the same forwards and backwards.
// A subsequence of a string is a new string generated from the original string with some characters (can be none) deleted without changing the relative order of the remaining characters.
// For example, "ace" is a subsequence of "abcde".
// Example 1:
// Input: s = "aabca"
// Output: 3
// Explanation: The 3 palindromic subsequences of length 3 are:
// - "aba" (subsequence of "aabca")
// - "aaa" (subsequence of "aabca")
// - "aca" (subsequence of "aabca")
// Example 2:
// Input: s = "adc"
// Output: 0
// Explanation: There are no palindromic subsequences of length 3 in "adc".
// Example 3:
// Input: s = "bbcbaba"
// Output: 4
// Explanation: The 4 palindromic subsequences of length 3 are:
// - "bbb" (subsequence of "bbcbaba")
// - "bcb" (subsequence of "bbcbaba")
// - "bab" (subsequence of "bbcbaba")
// - "aba" (subsequence of "bbcbaba")
// Constraints:
// 3 <= s.length <= 105
// s consists of only lowercase English letters.
class Solution
{
public:
int countPalindromicSubsequence(string s)
{
unordered_set<char> letters;
for (char c : s)
{
letters.insert(c);
}
int ans = 0;
for (char letter : letters)
{
int i = -1;
int j = 0;
for (int k = 0; k < s.size(); k++)
{
if (s[k] == letter)
{
if (i == -1)
{
i = k;
}
j = k;
}
}
unordered_set<char> between;
for (int k = i + 1; k < j; k++)
{
between.insert(s[k]);
}
ans += between.size();
}
return ans;
}
};
/*
Approach:
1. Create a set 'letters' to store unique characters from string s
2. For each unique letter in letters:
- Find first (i) and last (j) occurrence of that letter in string s
- Create a set 'between' to store unique characters between i and j
- Add all characters between i and j to 'between' set
- Add size of 'between' set to answer (represents number of unique palindromes possible with current letter)
3. Return final answer
Time Complexity: O(n * k) where n is length of string and k is number of unique characters
Space Complexity: O(k) where k is number of unique characters
*/