3498. Reverse Degree of a String¶
Intuition¶
The reversed alphabet assigns a -> 26, b -> 25, ..., z -> 1. For a
lowercase character ch, that value is available directly from its character
code:
$$\text{reverseValue}(ch) = \text{'z'} - ch + 1$$
Multiply it by the character's 1-indexed position and add the contribution to the answer. This follows the definition in one pass without building a reversed alphabet or a lookup table.
Approach: Direct Weighted Sum¶
- Traverse
sfrom left to right while tracking the 0-indexed positioni. - Compute the reversed-alphabet value as
'z' - ch + 1. - Multiply that value by
i + 1and add it to the running sum. - Return the sum after every character has been processed.
Why byte and index arithmetic are safe¶
The constraints contain only lowercase English letters, so each character is one
ASCII byte. Rust's bytes() and Go's byte offset from range therefore both
match the character position. With arbitrary Unicode text, Go's index would be
a byte offset rather than a character count, but that case is excluded here.
The largest possible answer is obtained from 1000 copies of a:
$$26 \cdot (1 + 2 + \cdots + 1000) = 13{,}013{,}000$$
That fits safely in Rust's i32 and Go's int.
Worked example¶
s = "abc":
| step | character | reversed value | string position | contribution | running sum |
|---|---|---|---|---|---|
| 1 | a |
26 |
1 |
26 |
26 |
| 2 | b |
25 |
2 |
50 |
76 |
| 3 | c |
24 |
3 |
72 |
148 |
The answer is 148. This example demonstrates both directions of the weighting:
reversed-alphabet values decrease while string-position multipliers increase.
Complexity¶
- Time complexity: $$O(n)$$, where
nis the length ofs— each character is processed exactly once. - Space complexity: $$O(1)$$ — only the running sum and loop variables are used.
Building a reversed alphabet and searching it for every character would add an unnecessary lookup step; direct character arithmetic computes each weight in constant time.
Code¶
Go¶
func reverseDegree(s string) int {
ans := 0
for index, ch := range s {
ans += (index + 1) * (int('z' - ch) + 1)
}
return ans
}
Rust¶
impl Solution {
pub fn reverse_degree(s: String) -> i32 {
let mut ans = 0;
for (i, ch) in s.bytes().enumerate() {
ans += (b'z' - ch + 1) as i32 * (i + 1) as i32;
}
ans
}
}
Python¶
class Solution:
def reverseDegree(self, s: str) -> int:
ans = 0
for i, ch in enumerate(s):
ans += (ord('z') - ord(ch) + 1) * (i + 1)
return ans
Test cases¶
| input | answer | what it exercises |
|---|---|---|
"abc" |
148 |
Example 1 — traced above |
"zaza" |
160 |
Example 2 — alternating minimum and maximum weights |
"a" |
26 |
single character with maximum reversed value |
"z" |
1 |
single character with minimum reversed value |
"aaa" |
156 |
repeated character; only position changes |
1000 copies of "z" |
500500 |
maximum length with the smallest weights |
1000 copies of "a" |
13013000 |
maximum length and maximum possible answer |
All three implementations were checked against an independent 26-entry lookup
table on the same 1127-case corpus: both examples, every string of length 1
through 4 over {a,b,c}, 1000 deterministic random lowercase strings of lengths
up to 1000, both single-character extremes, and the two maximum-length extremes.
All agreed on every case. Go was compiled and run with Go 1.21.6; Rust was built
without optimizations so overflow checks remained enabled.