LeetCode 3498: Reverse Degree of a String – Java Solution, Approach & Explanation

Calculate the reverse degree of a string by mapping each lowercase letter to its reversed alphabet position and multiplying it by its 1-based index.

Krishna Shrivastava
2 views
LinkedInGithubX
0
0
LeetCode 3498: Reverse Degree of a String – Java Solution, Approach & Explanation
Listen to articleAudio version
Ad

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:

a = 1
b = 2
c = 3
...
z = 26

For the reverse degree, the values are reversed:

a = 26
b = 25
c = 24
...
y = 2
z = 1

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:

s = "abc"

The reversed alphabet values are:

a → 26
b → 25
c → 24

The positions in the string are:

a → 1
b → 2
c → 3

Now multiply the two values:

a → 26 × 1 = 26
b → 25 × 2 = 50
c → 24 × 3 = 72

Therefore:

26 + 50 + 72 = 148

So the answer is:

148

Approach: Character Mapping with HashMap

The given solution creates a HashMap containing every lowercase character and its reverse-alphabet value.

The mapping looks like:

Character Reverse Value
--------------------------
a 26
b 25
c 24
...
x 3
y 2
z 1

Once the mapping is ready, the string is traversed from left to right.

For every character:

reverse value × position

is added to the answer.

Since the position in the problem is 1-indexed, the Java index i is converted using:

i + 1

Creating the Reverse Alphabet Mapping

The solution starts with:

int l = 26;

Then it iterates through the alphabet:

for (int i = 1; i <= 26; i++) {
char z = (char) (96 + i);
if (z == 'z') {
mp.put(z, 1);
} else {
mp.put(z, l);
}
l--;
}

The expression:

(char)(96 + i)

generates lowercase English letters.

For example:

i = 1 → 97 → 'a'
i = 2 → 98 → 'b'
i = 3 → 99 → 'c'

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:

for (int i = 0; i < s.length(); i++) {
int ind = mp.get(s.charAt(i));
sum += ind * (i + 1);
}

Here:

  1. s.charAt(i) → current character
  2. mp.get(...) → reverse-alphabet value
  3. i + 1 → 1-based position
  4. ind * (i + 1) → contribution of the character

All contributions are accumulated in sum.

Java Solution

class Solution {
public int reverseDegree(String s) {

// Store reverse alphabet values
HashMap<Character, Integer> mp = new HashMap<>();

int l = 26;

// Create mapping:
// a -> 26, b -> 25, ..., y -> 2, z -> 1
for (int i = 1; i <= 26; i++) {

char z = (char) (96 + i);

if (z == 'z') {
mp.put(z, 1);
} else {
mp.put(z, l);
}

l--;
}

int sum = 0;

// Calculate the reverse degree
for (int i = 0; i < s.length(); i++) {

// Reverse alphabet value of current character
int ind = mp.get(s.charAt(i));

// Position is 1-indexed
sum += ind * (i + 1);
}

return sum;
}
}

Dry Run

Consider:

s = "zaza"

The reverse alphabet values are:

z → 1
a → 26
z → 1
a → 26

Now calculate each contribution:

CharacterReverse ValuePositionProduct
z111
a26252
z133
a264104

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:

s.charAt(i) - 'a'

gives its normal zero-based alphabet offset.

The reverse value can therefore be calculated as:

26 - (character - 'a')

For example:

'a' → 26 - 0 = 26
'b' → 26 - 1 = 25
'c' → 26 - 2 = 24
'z' → 26 - 25 = 1

This removes the need for a HashMap.

The optimized version becomes:

class Solution {
public int reverseDegree(String s) {

int sum = 0;

for (int i = 0; i < s.length(); i++) {

int reverseValue = 26 - (s.charAt(i) - 'a');

sum += reverseValue * (i + 1);
}

return sum;
}
}

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:

s.charAt(i) - 'a'

is a common technique for converting:

a → 0
b → 1
c → 2
...
z → 25

From there, many alphabet-based mappings can be created without a HashMap.

In this problem, reversing that range gives:

26 - (s.charAt(i) - 'a')

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:

Reverse Value × String Position

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
Ai Assistant Kas