Skip to main content

Reverse Degree of a String - Solution & Explanation

EasyStringSimulation5 min readAsked at: Capgemini, Google
Practice this problem

Problem Statement

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.

 

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 <= 1000
  • s contains 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

Try this approach in the editor →

Detailed Complexity Analysis

ApproachTimeSpaceWhen to Use
Reverse Alphabet MappingO(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 is considered an easy problem. It focuses on basic string traversal, character arithmetic, and index-based calculations rather than complex algorithms or data structures.
Reverse Degree of a String Python/Java solution
The solution in Python or Java iterates through the string, calculates the reverse alphabet value using character arithmetic ('z' - c + 1), multiplies it by the index (i + 1), and accumulates the sum. The implementation is O(n) time and O(1) space in all major languages including Python, Java, C++, Go, and TypeScript.
How to solve Reverse Degree of a String in O(n)?
Iterate through the string from left to right. For each character at index i, compute its reverse alphabet value using ('z' - s[i] + 1), multiply it by (i + 1), and add it to a running total. Since every character is processed once, the algorithm achieves O(n) time complexity.
What is the best approach for Reverse Degree of a String?
The best approach is a single-pass simulation that computes each character's reverse alphabet value using ASCII arithmetic: ('z' - c + 1). Multiply that value by the character's 1-based index and accumulate the result. This solution runs in O(n) time and O(1) space.
Is Reverse Degree of a String asked at Google/Amazon/Meta?
Problems like Reverse Degree of a String appear in coding screens or online assessments as warm-up string manipulation questions. They test understanding of ASCII arithmetic, indexing, and basic iteration rather than advanced algorithms.
What data structure is used in Reverse Degree of a String?
No complex data structure is required. The problem is solved using simple string traversal and arithmetic operations. Some implementations use a small lookup array or map for reverse alphabet values, but it is not necessary.
What is the time complexity of Reverse Degree of a String?
The optimal solution runs in O(n) time because the algorithm scans the string once and performs constant-time arithmetic for each character. Space complexity is O(1) since only a few variables are required.

Ready to solve this problem?

Practice Reverse Degree of a String with our built-in code editor and test cases.

Practice on FleetCode