3889. Mirror Frequency Distance
Description
You are given a string s consisting of lowercase English letters and digits.
For each character, its mirror character is defined by reversing the order of its character set:
- For letters, the mirror of a character is the letter at the same position from the end of the alphabet.
- For example, the mirror of
'a'is'z', and the mirror of'b'is'y', and so on.
- For example, the mirror of
- For digits, the mirror of a character is the digit at the same position from the end of the range
'0'to'9'.- For example, the mirror of
'0'is'9', and the mirror of'1'is'8', and so on.
- For example, the mirror of
For each unique character c in the string:
- Let
mbe its mirror character. - Let
freq(x)denote the number of times characterxappears in the string. - Compute the absolute difference between their frequencies, defined as:
|freq(c) - freq(m)|
The mirror pairs (c, m) and (m, c) are the same and must be counted only once.
Return an integer denoting the total sum of these values over all such distinct mirror pairs.
Β
Example 1:
Input: s = "ab1z9"
Output: 3
Explanation:
For every mirror pair:
c | m | freq(c) | freq(m) | |freq(c) - freq(m)| |
|---|---|---|---|---|
| a | z | 1 | 1 | 0 |
| b | y | 1 | 0 | 1 |
| 1 | 8 | 1 | 0 | 1 |
| 9 | 0 | 1 | 0 | 1 |
Thus, the answer is 0 + 1 + 1 + 1 = 3.
Example 2:
Input: s = "4m7n"
Output: 2
Explanation:
c | m | freq(c) | freq(m) | |freq(c) - freq(m)| |
|---|---|---|---|---|
| 4 | 5 | 1 | 0 | 1 |
| m | n | 1 | 1 | 0 |
| 7 | 2 | 1 | 0 | 1 |
Thus, the answer is 1 + 0 + 1 = 2.βββββββ
Example 3:
Input: s = "byby"
Output: 0
Explanation:
c | m | freq(c) | freq(m) | |freq(c) - freq(m)| |
|---|---|---|---|---|
| b | y | 2 | 2 | 0 |
Thus, the answer is 0.
Β
Constraints:
1 <= s.length <= 5 * 105sconsists only of lowercase English letters and digits.
Solutions
Solution 1: Hash Table
We first use a hash table \(\textit{freq}\) to count the frequency of each character in string \(s\).
Then, we iterate over each key-value pair \((c, v)\) in \(\textit{freq}\), where \(c\) is the character and \(v\) is the number of times character \(c\) appears in string \(s\). For each character \(c\), we compute its mirror character \(m\) and calculate \(|freq(c) - freq(m)|\). To avoid counting mirror pairs twice, we use a hash set \(\textit{vis}\) to track already-visited characters.
Finally, we return the sum of absolute differences over all distinct mirror pairs.
The time complexity is \(O(n)\), where \(n\) is the length of string \(s\). The space complexity is \(O(|\Sigma|)\), where \(\Sigma\) is the set of distinct characters in string \(s\).
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | |
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 | |
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 | |
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 | |
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 | |