3412. Find Mirror Score of a String Medium

@problem@discussion
#Hash Table#String#Stack#Simulation



1/**
2 * [3412] Find Mirror Score of a String
3 *
4 * You are given a string s.
5 * 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'.
6 * Initially, all characters in the string s are unmarked.
7 * You start with a score of 0, and you perform the following process on the string s:
8 * 
9 * 	Iterate through the string from left to right.
10 * 	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.
11 * 	If no such index j exists for the index i, move on to the next index without making any changes.
12 * 
13 * Return the total score at the end of the process.
14 *  
15 * <strong class="example">Example 1:
16 * <div class="example-block">
17 * Input: <span class="example-io">s = "aczzx"</span>
18 * Output: <span class="example-io">5</span>
19 * Explanation:
20 * 
21 * 	i = 0. There is no index j that satisfies the conditions, so we skip.
22 * 	i = 1. There is no index j that satisfies the conditions, so we skip.
23 * 	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.
24 * 	i = 3. There is no index j that satisfies the conditions, so we skip.
25 * 	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.
26 * </div>
27 * <strong class="example">Example 2:
28 * <div class="example-block">
29 * Input: <span class="example-io">s = "abcdef"</span>
30 * Output: <span class="example-io">0</span>
31 * Explanation:
32 * For each index i, there is no index j that satisfies the conditions.
33 * </div>
34 *  
35 * Constraints:
36 * 
37 * 	1 <= s.length <= 10^5
38 * 	s consists only of lowercase English letters.
39 * 
40 */
41pub struct Solution {}
42
43// problem: https://leetcode.com/problems/find-mirror-score-of-a-string/
44// discuss: https://leetcode.com/problems/find-mirror-score-of-a-string/discuss/?currentPage=1&orderBy=most_votes&query=
45
46// submission codes start here
47
48impl Solution {
49    pub fn calculate_score(s: String) -> i64 {
50        
51    }
52}
53
54// submission codes end
55
56#[cfg(test)]
57mod tests {
58    use super::*;
59
60    #[test]
61    fn test_3412() {
62    }
63}
64


Back
© 2025 bowen.ge All Rights Reserved.