[LeetCode] Reverse Degree of a String

3498. Reverse Degree of a String

Given a string s, calculate its reverse degree.

The reverse degree is calculated as follows:

  1. For each character, multiply its position in the reversed alphabet ('a' = 26, 'b' = 25, …, 'z' = 1) with its position in the string (1-indexed).
  2. Sum these products for all characters in the string.

Return the reverse degree of s.

1
2
3
4
5
6
7
func reverseDegree(s string) int {
res := 0
for i := 0; i < len(s); i++ {
res += (i + 1) * (26 - int(s[i]-'a'))
}
return res
}
Author: Song Hayoung
Link: https://songhayoung.github.io/2025/03/30/PS/LeetCode/reverse-degree-of-a-string/
Copyright Notice: All articles in this blog are licensed under CC BY-NC-SA 4.0 unless stating additionally.