We define the mirror of a letter in the English alphabet as its corresponding letter when the alphabet is reversed. For example, the mirror of
'a' is 'z', and the mirror of 'y' is 'b'.
Initially, all characters in the string s are unmarked.
You start with a score of 0, and you perform the following process on the string s:
Iterate through the string from left to right.
At each index i, find the closest unmarked index j such that j < i and s[j] is the mirror of s[i]. Then, mark both indices i and j, and add the value i - j to the total score.
If no such index j exists for the index i, move on to the next index without making any changes.
Input: s ="aczzx"Output: 5Explanation:
*`i = 0`. There is no index `j` that satisfies the conditions, so we skip.*`i = 1`. There is no index `j` that satisfies the conditions, so we skip.*`i = 2`. The closest index `j` that satisfies the conditions is`j = 0`, so we mark both indices 0 and 2, and then add `2 - 0 = 2` to the score.*`i = 3`. There is no index `j` that satisfies the conditions, so we skip.*`i = 4`. The closest index `j` that satisfies the conditions is`j = 1`, so we mark both indices 1 and 4, and then add `4 - 1 = 3` to the score.
We need to efficiently find the closest unmarked mirror character to the left for each character. By mapping each character to its mirror and using a stack for each mirror character, we can quickly find and mark pairs as we iterate.
classSolution {
public:int mirrorScore(string s) {
unordered_map<char, char> mirror;
for (char c ='a'; c <='z'; ++c) mirror[c] ='z'- (c -'a');
unordered_map<char, stack<int>> stks;
int ans =0;
vector<bool> marked(s.size(), false);
for (int i =0; i < s.size(); ++i) {
char m = mirror[s[i]];
if (!stks[m].empty()) {
int j = stks[m].top(); stks[m].pop();
ans += i - j;
} else {
stks[s[i]].push(i);
}
}
return ans;
}
};
classSolution {
publicintmirrorScore(String s) {
Map<Character, Character> mirror =new HashMap<>();
for (char c ='a'; c <='z'; c++) mirror.put(c, (char)('z'- (c -'a')));
Map<Character, Deque<Integer>> stks =new HashMap<>();
int ans = 0;
for (int i = 0; i < s.length(); i++) {
char m = mirror.get(s.charAt(i));
stks.putIfAbsent(m, new ArrayDeque<>());
stks.putIfAbsent(s.charAt(i), new ArrayDeque<>());
if (!stks.get(m).isEmpty()) {
int j = stks.get(m).pop();
ans += i - j;
} else {
stks.get(s.charAt(i)).push(i);
}
}
return ans;
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
classSolution {
funmirrorScore(s: String): Int {
val mirror = ('a'..'z').associateWith { 'z' - (it - 'a') }
val stks = mutableMapOf<Char, ArrayDeque<Int>>()
var ans = 0for (i in s.indices) {
val m = mirror[s[i]]!! stks.putIfAbsent(m, ArrayDeque())
stks.putIfAbsent(s[i], ArrayDeque())
if (stks[m]!!.isNotEmpty()) {
val j = stks[m]!!.removeLast()
ans += i - j
} else {
stks[s[i]]!!.addLast(i)
}
}
return ans
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
classSolution:
defmirrorScore(self, s: str) -> int:
mirror = {chr(ord('a')+i): chr(ord('z')-i) for i in range(26)}
stks = {c: [] for c in mirror}
ans =0for i, c in enumerate(s):
m = mirror[c]
if stks[m]:
j = stks[m].pop()
ans += i - j
else:
stks[c].append(i)
return ans
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
impl Solution {
pubfnmirror_score(s: String) -> i32 {
let mirror: Vec<char>= (0..26).map(|i| (b'z'- i) aschar).collect();
letmut stks: Vec<Vec<usize>>=vec![vec![]; 26];
let s = s.as_bytes();
letmut ans =0;
for (i, &c) in s.iter().enumerate() {
let idx = (c -b'a') asusize;
let m = (b'z'- (c -b'a')) asusize-b'a'asusize;
if!stks[m].is_empty() {
let j = stks[m].pop().unwrap();
ans += i - j;
} else {
stks[idx].push(i);
}
}
ans asi32 }
}