3412. Find Mirror Score of a String
3412. Find Mirror Score of a String
Description
You are given a string s
.
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 indexj
such thatj < i
ands[j]
is the mirror ofs[i]
. Then, mark both indicesi
andj
, and add the valuei - j
to the total score. - If no such index
j
exists for the indexi
, move on to the next index without making any changes.
Return the total score at the end of the process.
Example 1:
1 | Input: s = "aczzx" |
Explanation:
i = 0
. There is no indexj
that satisfies the conditions, so we skip.i = 1
. There is no indexj
that satisfies the conditions, so we skip.i = 2
. The closest indexj
that satisfies the conditions isj = 0
, so we mark both indices 0 and 2, and then add2 - 0 = 2
to the score.i = 3
. There is no indexj
that satisfies the conditions, so we skip.i = 4
. The closest indexj
that satisfies the conditions isj = 1
, so we mark both indices 1 and 4, and then add4 - 1 = 3
to the score.
Example 2:
1 | Input: s = "abcdef" |
Explanation:
For each index i
, there is no index j
that satisfies the conditions.
Constraints:
1 <= s.length <= 10^5
s
consists only of lowercase English letters.
Hints/Notes
- 2025/01/13
- stack
- 0x3Fâs solution(checked)
- Weekly Contest 431
Solution
Language: C++
1 | class Solution { |