Skip to main content

Reverse Degree of a String - Video Solutions

EasyStringSimulation

3498. Reverse Degree of a String | Biweekly Contest 153 | Strings | O(n) | Leetcode

Rapid Syntax
4:55443 views
9 video solutions available

Reverse Degree of a String - Video Solution

Watch 9 video solutions for Reverse Degree of a String, a easy level problem involving String, Simulation. This walkthrough by Rapid Syntax has 443 views views. Want to try solving it yourself? Practice on FleetCode or read the detailed text solution.

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.
Read full problem with examples

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.

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