Reverse Degree of a String

Easy
#3110Time: O(N), where N is the length of the string `s`. Populating the map takes constant time O(1) as the alphabet size is fixed at 26. The main part of the algorithm is the loop that iterates through the string once.Space: O(1). We use a HashMap to store the values for the 26 lowercase English letters. Since the size of the alphabet is constant, the space required for the map does not grow with the input string size.1 company
Data structures
Companies

Prompt

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.

Approaches

2 approaches with complexity analysis and trade-offs.

This approach involves pre-calculating the reverse alphabetical value for each character and storing them in a HashMap. We then iterate through the input string, and for each character, we look up its value in the map, multiply it by its 1-indexed position, and add the result to a running total.

Algorithm

  • Initialize a HashMap<Character, Integer>.
  • Populate the map by iterating from 'a' to 'z', mapping each character c to its value 26 - (c - 'a').
  • Initialize a sum variable reverseDegree to 0.
  • Loop through the input string s from index i = 0 to s.length() - 1.
  • Inside the loop, get the character currentChar and retrieve its value from the map.
  • Multiply the character's value by its 1-indexed position (i + 1).
  • Add the product to reverseDegree.
  • Return reverseDegree after the loop.

Walkthrough

In this method, we first set up a helper data structure, a HashMap, to act as a lookup table. This map stores each lowercase letter and its corresponding reverse alphabetical value. For example, 'a' maps to 26, 'b' to 25, and so on. Once the map is populated, we process the input string. We iterate through the string character by character, using the character's 1-indexed position. For each character, we fetch its pre-calculated value from the map, multiply it by its position, and accumulate the result in a total sum. This separation of concerns can make the code's intent clearer, though it comes at the cost of extra space and minor performance overhead.

import java.util.HashMap;import java.util.Map; class Solution {    public int reverseDegree(String s) {        Map<Character, Integer> alphabetValues = new HashMap<>();        for (char c = 'a'; c <= 'z'; c++) {            alphabetValues.put(c, 26 - (c - 'a'));        }         int reverseDegree = 0;        for (int i = 0; i < s.length(); i++) {            char currentChar = s.charAt(i);            int charValue = alphabetValues.get(currentChar);            int position = i + 1;            reverseDegree += charValue * position;        }        return reverseDegree;    }}

Complexity

Time

O(N), where N is the length of the string `s`. Populating the map takes constant time O(1) as the alphabet size is fixed at 26. The main part of the algorithm is the loop that iterates through the string once.

Space

O(1). We use a HashMap to store the values for the 26 lowercase English letters. Since the size of the alphabet is constant, the space required for the map does not grow with the input string size.

Trade-offs

Pros

  • The logic is very explicit, making the code easy to read and understand.

  • Separates the concern of calculating character values from the main summation logic.

Cons

  • Uses extra space for the HashMap.

  • Slightly less performant due to the overhead of hash calculations and map lookups compared to direct arithmetic calculation.

Solutions

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;  }}

Video walkthrough

Newsletter

One sharp idea, every week

System design and interview prep — short enough to finish.

No spam. Unsubscribe anytime.

Practice

Same difficulty — related problems to reinforce the pattern.