3498. Reverse Degree of a String

1 min read

LeetCode Daily Problem: Reverse Degree of a String

On this page

Original problem on LeetCode

Approach: Iterate

The statement already defines each character's degree. Just scan the string once and add up the degrees.

Code

python
class Solution:
    def reverseDegree(self, s: str) -> int:
        return sum(((26 - (ord(c) - ord('a')))*(i+1) for i, c in enumerate(list(s))))

Complexity

  • Time: O(n) — we traverse the string once.
  • Space: O(1) — no extra space is used.
TagsSimulation

Back to all posts