#3498
Easy Algorithms Reverse degree of a string
String Simulation
88.6% acceptance
Feb 25, 2026
82
5
Given a string s, calculate its reverse degree.
The reverse degree is calculated as follows:
For each character, multiply its position in the reversed alphabet ('a' = 26, 'b' = 25, ..., 'z' = 1) with its position in the string (1-indexed).
Sum these products for all characters in the string.
Return the reverse degree of s.
Solution
Rust
Time O(n)
Space O(1)
impl Solution {
pub fn reverse_degree(s: String) -> i64 {
s.bytes().enumerate().map(|(i, c)| {
let rev_pos = (b'z' - c + 1) as i64;
let str_pos = (i + 1) as i64;
rev_pos * str_pos
}).sum()
}
}