← All solutions
September 20Easy

Reverse degree of a string

  • String
  • Simulation

Problem

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

class Solution:
    def reverseDegree(self, s: str) -> int:
        res = 0
        base = ord('z') + 1
        for i in range(len(s)):
            res += (i + 1) * (base - ord(s[i]))
        return res