Reverse Degree of a String - Problem

Given a string s, calculate its reverse degree.

The reverse degree is calculated as follows:

  • For each character, multiply its position in the reversed alphabet ('a' = 26, 'b' = 25, ..., 'z' = 1) with its position in the string (1-indexed).
  • Sum these products for all characters in the string.

Return the reverse degree of s.

Input & Output

Example 1 — Basic Case
$ Input: s = "abc"
Output: 148
💡 Note: a=26×1=26, b=25×2=50, c=24×3=72. Total: 26+50+72=148
Example 2 — Single Character
$ Input: s = "z"
Output: 1
💡 Note: z has reverse value 1, position 1: 1×1=1
Example 3 — Repeated Characters
$ Input: s = "aa"
Output: 78
💡 Note: First a: 26×1=26, second a: 26×2=52. Total: 26+52=78

Constraints

  • 1 ≤ s.length ≤ 1000
  • s consists of lowercase English letters only

Visualization

Tap to expand
Reverse Degree Calculation ProcessInput: "abc"abcReverse values:262524Positions:123Products:265072Final Sum: 148
Understanding the Visualization
1
Input String
String with lowercase letters
2
Calculate Products
For each char: reverse_alphabet_value × position
3
Sum Results
Add all products to get final degree
Key Takeaway
🎯 Key Insight: Use formula 27-(char-'a') to efficiently get reverse alphabet values
Asked in
Google 15 Microsoft 12
12.0K Views
Medium Frequency
~8 min Avg. Time
450 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