Reverse Degree of a String | Reverse Degree of a String Solution | CPP
IntuitionThe problem asks us to calculate a custom score for a string where:
Each character’s score is based on its position in the reversed English alphabet.
T
# Intuition
<!-- Describe your first thoughts on how to solve this problem. -->
The problem asks us to calculate a custom score for a string where:
Each character’s score is based on its position in the reversed English alphabet.
That means:
'a' = 26, 'b' = 25, ..., 'z' = 1

Multiply this reversed position with the character's 1-based index in the string and sum all such values for the final result.

So we’re combining two concepts:

Character encoding (a to z) & Positional weight



# Approach
<!-- Describe your approach to solving the problem. -->
1. Initialize a variable total to 0 for storing the final score.
2. Traverse the string character by character using a loop.
3. For each character:
   Get its 1-based position in the string: i + 1
   Calculate its reversed alphabet index using:
    26 - (s[i] - 'a')
4. Multiply the reversed index with the position.
5. Add the result to the total score.
6. After the loop ends, return the final value.

# Complexity
- Time complexity: O(n) where n is the length of the string
<!-- Add your time complexity here, e.g. $$O(n)$$ -->

- Space complexity: O(1) constant space.
<!-- Add your space complexity here, e.g. $$O(n)$$ -->


```cpp []
class Solution {
public:
    int reverseDegree(string s) {
        int total = 0;
        for (int i = 0; i < s.length(); ++i) {
            int position = i + 1; 
            int reversePos = 26 - (s[i] - 'a'); 
            total += reversePos * position;
        }
        return total; 
    }       
};
```
['String', 'Simulation', 'C++']