Reverse Degree of a String - Solution & Explanation
Problem Statement
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.
Example 1:
Input: s = "abc"
Output: 148
Explanation:
| Letter | Index in Reversed Alphabet | Index in String | Product |
|---|---|---|---|
'a' |
26 | 1 | 26 |
'b' |
25 | 2 | 50 |
'c' |
24 | 3 | 72 |
The reversed degree is 26 + 50 + 72 = 148.
Example 2:
Input: s = "zaza"
Output: 160
Explanation:
| Letter | Index in Reversed Alphabet | Index in String | Product |
|---|---|---|---|
'z' |
1 | 1 | 1 |
'a' |
26 | 2 | 52 |
'z' |
1 | 3 | 3 |
'a' |
26 | 4 | 104 |
The reverse degree is 1 + 52 + 3 + 104 = 160.
Constraints:
1 <= s.length <= 1000scontains only lowercase English letters.
Approach Overview
Problem Overview: You are given a lowercase string. Each character contributes a value based on its reverse alphabet position (z = 1, y = 2, ..., a = 26). Multiply that value by the character's 1-based index in the string and sum the results. The final sum is called the reverse degree of the string.
Approach 1: Reverse Alphabet Mapping (O(n) time, O(1) space)
The most direct solution builds a mapping from characters to their reverse alphabet values. For example, 'a' → 26, 'b' → 25, ..., 'z' → 1. Iterate through the string once, look up each character's reverse value, multiply it by its 1-based index, and accumulate the result. This approach is straightforward and easy to reason about, especially if you explicitly construct a dictionary or array of size 26. The algorithm scans the string once, so the time complexity is O(n). The extra lookup table uses constant memory, giving O(1) space complexity.
Approach 2: Direct Character Arithmetic (Simulation) (O(n) time, O(1) space)
A cleaner implementation avoids any lookup structure and calculates the reverse value using ASCII arithmetic. For a character c, the reverse position is 'z' - c + 1. While iterating through the string, compute this value directly, multiply it by the index (i + 1), and add it to the running total. This approach is still a single pass through the string, so the time complexity remains O(n) with O(1) extra space. It is typically preferred in interviews because it shows you understand character encoding and avoids unnecessary data structures.
The algorithm is essentially a simple string traversal combined with lightweight simulation. Each iteration performs constant-time arithmetic and updates a running sum.
Recommended for interviews: The direct character arithmetic approach. Interviewers expect you to recognize that reverse alphabet values can be derived from ASCII math instead of building a lookup table. Mentioning the mapping idea first can show your thought process, but implementing the O(n) single-pass simulation demonstrates cleaner problem-solving.
Solution
We can simulate the reverse degree of each character in the string. For each character, calculate its position in the reverse alphabet, multiply it by its position in the string, and then sum up all the results.
Time complexity is O(n), where n is the length of the string. Space complexity is O(1).
Code
Python
Java
C++
Go
TypeScript
Detailed Complexity Analysis
| Approach | Time | Space | When to Use |
|---|---|---|---|
| Reverse Alphabet Mapping | O(n) | O(1) | When clarity matters or when demonstrating the reverse alphabet mapping explicitly |
| Direct Character Arithmetic (Simulation) | O(n) | O(1) | Preferred approach in interviews; avoids extra structures and uses simple ASCII math |
Video Solution
3498. Reverse Degree of a String | Biweekly Contest 153 | Strings | O(n) | Leetcode • Rapid Syntax • 443 views views
Watch 8 more video solutions →Frequently Asked Questions
Is Reverse Degree of a String easy or hard?
Reverse Degree of a String Python/Java solution
How to solve Reverse Degree of a String in O(n)?
What is the best approach for Reverse Degree of a String?
Is Reverse Degree of a String asked at Google/Amazon/Meta?
What data structure is used in Reverse Degree of a String?
What is the time complexity of Reverse Degree of a String?
Ready to solve this problem?
Practice Reverse Degree of a String with our built-in code editor and test cases.
Practice on FleetCodeTable of Contents
Practice this problem
Open in Editor