lcb-0172
1.000
Proposed solution
I'll solve this step by step.
## Problem Understanding
1. **Reverse degree calculation**: For each character in the string
- Find its position in the "reversed alphabet" where 'a'=26, 'b'=25, ..., 'z'=1
- Multiply this by its position in the string (1-indexed)
- Sum all these products
2. **Examples walkthrough**:
- For "abc":
- 'a' (reversed pos 26) × 1st position (1) = 26
- 'b' (reversed pos 25) × 2nd position (2) = 50
- 'c' (reversed pos 24) × 3rd position (3) = 72
- Total = 26 + 50 + 72 = 148
## Approach
1. Create a mapping for reversed alphabet positions
2. Iterate through each character with its 1-indexed position
3. Calculate the product of reversed alphabet position and string position
4. Sum all products
## Implementation
```python
class Solution:
def reverseDegree(self, s: str) -> int:
# Calculate reverse degree
total = 0
for i, char in enumerate(s):
# Position in reversed alphabet: 'a'=26, 'b'=25, ..., 'z'=1
reversed_pos = ord('z') - ord(char) + 1
# Position in string (1-indexed)
string_pos = i + 1
# Add product to total
total += reversed_pos * string_pos
return total
```
Let me trace through the examples to verify:
**Example 1: s = "abc"**
- i=0, char='a': reversed_pos = 26, string_pos = 1, product = 26×1 = 26
- i=1, char='b': reversed_pos = 25, string_pos = 2, product = 25×2 = 50
- i=2, char='c': reversed_pos = 24, string_pos = 3, product = 24×3 = 72
- Total = 26 + 50 + 72 = 148 ✓
**Example 2: s = "zaza"**
- i=0, char='z': reversed_pos = 1, string_pos = 1, product = 1×1 = 1
- i=1, char='a': reversed_pos = 26, string_pos = 2, product = 26×2 = 52
- i=2, char='z': reversed_pos = 1, string_pos = 3, product = 1×3 = 3
- i=3, char='a': reversed_pos = 26, string_pos = 4, product = 26×4 = 104
- Total = 1 + 52 + 3 + 104 = 160 ✓
The solution looks correct!Test output (stdout)
.. [100%] 2 passed in 0.01s
System prompt
You are an expert programmer. Solve the task exactly as specified. Output your solution as fenced code blocks using the required file name(s) and the exact function/type signatures requested. Prefer correctness; do not include prose outside code unless asked.