Reverse Degree of a String - Problem

You're given a string s consisting of lowercase English letters, and your task is to calculate its reverse degree - a unique numerical fingerprint of the string.

The reverse degree calculation works as follows:

  • For each character in the string, find its reverse alphabet position: 'a' = 26, 'b' = 25, 'c' = 24, ..., 'z' = 1
  • Multiply this reverse position by the character's 1-indexed position in the string
  • Sum all these products to get the final reverse degree

Example: For string "abc":

  • 'a' at position 1: 26 × 1 = 26
  • 'b' at position 2: 25 × 2 = 50
  • 'c' at position 3: 24 × 3 = 72
  • Reverse degree = 26 + 50 + 72 = 148

This problem tests your ability to work with string indexing and character manipulation - fundamental skills for many coding interviews!

Input & Output

example_1.py — Basic String
$ Input: s = "abc"
Output: 148
💡 Note: For 'a' at position 1: 26 × 1 = 26. For 'b' at position 2: 25 × 2 = 50. For 'c' at position 3: 24 × 3 = 72. Total: 26 + 50 + 72 = 148.
example_2.py — Single Character
$ Input: s = "z"
Output: 1
💡 Note: For 'z' at position 1: reverse alphabet position is 1, so 1 × 1 = 1.
example_3.py — Repeated Characters
$ Input: s = "aa"
Output: 78
💡 Note: First 'a' at position 1: 26 × 1 = 26. Second 'a' at position 2: 26 × 2 = 52. Total: 26 + 52 = 78.

Constraints

  • 1 ≤ s.length ≤ 1000
  • s consists of lowercase English letters only
  • No empty strings - minimum length is 1

Visualization

Tap to expand
Secret Code Scoring: "hello"hDanger: 19Position: 1Score: 19eDanger: 22Position: 2Score: 44lDanger: 15Position: 3Score: 45lDanger: 15Position: 4Score: 60oDanger: 12Position: 5Score: 60Total Score19 + 44 + 45 + 60 + 60= 228Key Formula:Reverse Alphabet Position = 27 - (character - 'a' + 1)Character Score = Reverse Position × String PositionTotal Reverse Degree = Sum of all character scoresTime: O(n) | Space: O(1)
Understanding the Visualization
1
Assign Danger Values
Each letter gets a danger value: 'a'=26 (most dangerous), 'b'=25, ..., 'z'=1 (least dangerous)
2
Calculate Position Importance
Each position in the string has increasing importance: position 1, position 2, position 3, etc.
3
Compute Individual Scores
For each character: multiply its danger value by its position importance
4
Sum All Scores
Add up all individual character scores to get the total reverse degree
Key Takeaway
🎯 Key Insight: We can calculate each character's reverse alphabet position directly using the formula 27 - (char - 'a' + 1), avoiding any lookup tables or complex calculations.
Asked in
Google 15 Amazon 12 Microsoft 8 Meta 6
38.2K Views
Medium Frequency
~8 min Avg. Time
1.5K Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen