Find Mirror Score of a String - Problem
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 indexjsuch thatj < iands[j]is the mirror ofs[i]. - Then, mark both indices
iandj, and add the valuei - jto the total score. - If no such index
jexists for the indexi, move on to the next index without making any changes.
Return the total score at the end of the process.
Input & Output
Example 1 — Basic Mirror Matching
$
Input:
s = "abccba"
›
Output:
0
💡 Note:
Process each character: 'a' at 0 (no mirror 'z' before), 'b' at 1 (no mirror 'y' before), 'c' at 2 (no mirror 'x' before), 'c' at 3 (no mirror 'x' before), 'b' at 4 (no mirror 'y' before), 'a' at 5 (no mirror 'z' before). No matches found, so score = 0.
Example 2 — No Mirrors Available
$
Input:
s = "abc"
›
Output:
0
💡 Note:
No character has its mirror appearing before it in the string, so no matches are made and score remains 0
Example 3 — Multiple Mirror Matches
$
Input:
s = "azba"
›
Output:
1
💡 Note:
'a' at 0 (no mirror 'z' before), 'z' at 1 finds mirror 'a' at 0, score += 1-0 = 1, 'b' at 2 (no mirror 'y' before), 'a' at 3 (mirror 'z' at 1 is already marked). Total score = 1.
Constraints
- 1 ≤ s.length ≤ 105
- s consists of lowercase English letters only
Visualization
Tap to expand
💡
Explanation
AI Ready
💡 Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code