Talentd
FleetCode
Make and follow sheets.Sign in

Reverse Degree of a String solution and explanation

EasySimulationString3 min read91.9% acceptanceUpdated 24 Mar 2026
3498. Reverse Degree of a String | Biweekly Contest 153 | Strings | O(n) | LeetcodeRapid Syntax · 4:55 · 443 viewsWatch on YouTube
8 more videos on this problem
  1. 3498. Reverse Degree of a String | Biweekly Contest 153 | Strings | O(n) | LeetcodeRapid Syntax · 4:55 · 443 views
  2. 3498. Reverse Degree of a String (Leetcode Easy)Programming Live with Larry · 2:52 · 252 views
  3. LeetCode 3498: Reverse Degree Of A String | C# Solution | Dynamic ProgrammingCode Scribbler · 2:58 · 177 views
  4. Reverse Degree of a String | Leetcode Solution | Leetcode Contest | @Solution_spotSolution Spot · 0:23 · 61 views
  5. Reverse Degree of a String LeetCode - 3498BugToBuild · 2:06 · 22 views
  6. LeetCode 3498 Explained | Reverse Degree of a String | C++ Coding Solution🔥Qubit Coderz · 6:36 · 19 views
  7. LeetCode 3498 | Reverse Degree of a String 🔥 | Easy C++ Solution + ExplanationManan patel · 3:45 · 5 views
  8. 3498. Reverse Degree of a String | LeetCode Problem Solving Playlist | Easy and Optimized SolutionHet Roy · 5:03 · 2 views
  9. Leet Code : 3498.Reverse Degree of a String In ArabicStack Mind · 3:45 · 2 views

The problem

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).

class Solution:
    def reverseDegree(self, s: str) -> int:
        ans = 0
        for i, c in enumerate(s, 1):
            x = 26 - (ord(c) - ord("a"))
            ans += i * x
        return ans
class Solution {
    public int reverseDegree(String s) {
        int n = s.length();
        int ans = 0;
        for (int i = 1; i <= n; ++i) {
            int x = 26 - (s.charAt(i - 1) - 'a');
            ans += i * x;
        }
        return ans;
    }
}
class Solution {
public:
    int reverseDegree(string s) {
        int n = s.length();
        int ans = 0;
        for (int i = 1; i <= n; ++i) {
            int x = 26 - (s[i - 1] - 'a');
            ans += i * x;
        }
        return ans;
    }
};
function reverseDegree(s: string): number {
    let ans = 0;
    for (let i = 1; i <= s.length; ++i) {
        const x = 26 - (s.charCodeAt(i - 1) - 'a'.charCodeAt(0));
        ans += i * x;
    }
    return ans;
}
func reverseDegree(s string) (ans int) {
	for i, c := range s {
		x := 26 - int(c-'a')
		ans += (i + 1) * x
	}
	return
}
Try this approach in the editor

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

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.

Comments

Now write it yourselfClose this page and solve Reverse Degree of a String in the editor, without looking.
Practise Reverse Degree of a String