Introduction
LeetCode 3498, Reverse Degree of a String, is a simple string and character-mapping problem.
The idea is straightforward: every lowercase English letter is assigned a value according to its position in the reversed alphabet.
Normally:
For the reverse degree, the values are reversed:
Each character's reverse-alphabet value is then multiplied by its 1-based position in the string.
Finally, all these products are added together.
Question Link
LeetCode 3498 – Reverse Degree of a String
Understanding the Problem
Consider:
The reversed alphabet values are:
The positions in the string are:
Now multiply the two values:
Therefore:
So the answer is:
Approach: Character Mapping with HashMap
The given solution creates a HashMap containing every lowercase character and its reverse-alphabet value.
The mapping looks like:
Once the mapping is ready, the string is traversed from left to right.
For every character:
is added to the answer.
Since the position in the problem is 1-indexed, the Java index i is converted using:
Creating the Reverse Alphabet Mapping
The solution starts with:
Then it iterates through the alphabet:
The expression:
generates lowercase English letters.
For example:
The variable l starts at 26 and decreases after every character.
This produces the reversed alphabet mapping.
Calculating the Reverse Degree
After building the map, the solution traverses the string:
Here:
s.charAt(i)→ current charactermp.get(...)→ reverse-alphabet valuei + 1→ 1-based positionind * (i + 1)→ contribution of the character
All contributions are accumulated in sum.
Java Solution
Dry Run
Consider:
The reverse alphabet values are:
Now calculate each contribution:
| Character | Reverse Value | Position | Product |
z | 1 | 1 | 1 |
a | 26 | 2 | 52 |
z | 1 | 3 | 3 |
a | 26 | 4 | 104 |
Therefore:
1 + 52 + 3 + 104 = 160
So:
Answer = 160
A Simpler Way to Find the Reverse Value
A HashMap works, but the reverse-alphabet value can actually be calculated directly from the character.
For a lowercase character:
gives its normal zero-based alphabet offset.
The reverse value can therefore be calculated as:
For example:
This removes the need for a HashMap.
The optimized version becomes:
This version directly derives the required value from the character.
Complexity Analysis
Let n be the length of the string.
Building the alphabet map takes constant time because there are always only 26 lowercase letters.
The string is then traversed once.
Time Complexity: O(n)
Space Complexity: O(1)
Although the HashMap contains 26 entries, 26 is a fixed constant, so its space usage is O(1).
The direct-mapping version also uses:
Space Complexity: O(1)
and avoids the extra map entirely.
Interview Tip
Whenever a problem involves the alphabet, first check whether the required character value can be derived mathematically.
For example:
is a common technique for converting:
From there, many alphabet-based mappings can be created without a HashMap.
In this problem, reversing that range gives:
which directly produces the required reverse-alphabet value.
Conclusion
LeetCode 3498 is a straightforward string traversal problem built around character mapping and positional multiplication.
The given solution first creates a reverse-alphabet mapping using a HashMap, then calculates each character's contribution using its 1-based position.
The key formula is:
A useful optimization is to calculate the reverse value directly from the character instead of storing all 26 mappings.
The final solution requires only a single traversal of the string and runs in:
O(n) time O(1) space




