Reverse Degree of a String

Given a string `s` of lowercase letters, compute its reverse degree. Number the alphabet backwards, so 'a' is 26, 'b' is 25 and so on down to 'z' = 1. For every character, multiply that reversed value by the character's 1-indexed position in `s`, and add up all the products. Return the sum.

30:00

Reverse Degree of a String
easy
Free written solution
Topics
Companies

Given a string s of lowercase letters, compute its reverse degree. Number the alphabet backwards, so 'a' is 26, 'b' is 25 and so on down to 'z' = 1. For every character, multiply that reversed value by the character's 1-indexed position in s, and add up all the products. Return the sum.

Example 1:
Input: {"s":"abc"}
Output: 148
Constraints:
  • 1≤s.length≤10001 \leq s.\text{length} \leq 1000

  • s contains only lowercase English letters.

Test Cases
Input (Stdin)
Expected Output (Stdout)
Execution Result

Run code to see results.

Input
arr ={"s":"abc"}

Flip the alphabet so 'a' is worth 26 and 'z' is worth 1, then weigh each letter of "abc" by its position.

the alphabet counted backwards

a26b25c24d23e22f21g20h19i18j17k16l15m14n13o12p11q10r9s8t7u6v5w4x3y2z1

s: position (weight) over letter, product below

x1

a

26

x2

b

50

x3

c

72

this letter

-

total

0

0 of 3 letters counted

Variables
VariableValue
total0
DepthFunction Call
Stack empty
0/8