Off-by-one on range boundaries
Wrong move: Loop endpoints miss first/last candidate.
Usually fails on: Fails on minimal arrays and exact-boundary answers.
Fix: Re-derive loops from inclusive/exclusive ranges before coding.
Build confidence with an intuition-first walkthrough focused on core interview patterns fundamentals.
Given a string s, calculate its reverse degree.
The reverse degree is calculated as follows:
'a' = 26, 'b' = 25, ..., 'z' = 1) with its position in the string (1-indexed).Return the reverse degree of s.
Example 1:
Input: s = "abc"
Output: 148
Explanation:
| Letter | Index in Reversed Alphabet | Index in String | Product |
|---|---|---|---|
'a' |
26 | 1 | 26 |
'b' |
25 | 2 | 50 |
'c' |
24 | 3 | 72 |
The reversed degree is 26 + 50 + 72 = 148.
Example 2:
Input: s = "zaza"
Output: 160
Explanation:
| Letter | Index in Reversed Alphabet | Index in String | Product |
|---|---|---|---|
'z' |
1 | 1 | 1 |
'a' |
26 | 2 | 52 |
'z' |
1 | 3 | 3 |
'a' |
26 | 4 | 104 |
The reverse degree is 1 + 52 + 3 + 104 = 160.
Constraints:
1 <= s.length <= 1000s contains only lowercase English letters.Problem summary: 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.
Start with the most direct exhaustive search. That gives a correctness anchor before optimizing.
Pattern signal: General problem-solving
"abc"
"zaza"
Source-backed implementations are provided below for direct study and interview prep.
// Accepted solution for LeetCode #3498: Reverse Degree of a String
class Solution {
public int reverseDegree(String s) {
int n = s.length();
int ans = 0;
for (int i = 1; i <= n; ++i) {
int x = 26 - (s.charAt(i - 1) - 'a');
ans += i * x;
}
return ans;
}
}
// Accepted solution for LeetCode #3498: Reverse Degree of a String
func reverseDegree(s string) (ans int) {
for i, c := range s {
x := 26 - int(c-'a')
ans += (i + 1) * x
}
return
}
# Accepted solution for LeetCode #3498: Reverse Degree of a String
class Solution:
def reverseDegree(self, s: str) -> int:
ans = 0
for i, c in enumerate(s, 1):
x = 26 - (ord(c) - ord("a"))
ans += i * x
return ans
// Accepted solution for LeetCode #3498: Reverse Degree of a String
fn reverse_degree(s: String) -> i32 {
s.bytes().enumerate().fold(0, |acc, (i, b)| {
let v = (b'z' - b + 1) as i32;
acc + (i + 1) as i32 * v
})
}
fn main() {
let ret = reverse_degree("abc".to_string());
println!("ret={ret}");
}
#[test]
fn test() {
assert_eq!(reverse_degree("abc".to_string()), 148);
assert_eq!(reverse_degree("zaza".to_string()), 160);
}
// Accepted solution for LeetCode #3498: Reverse Degree of a String
function reverseDegree(s: string): number {
let ans = 0;
for (let i = 1; i <= s.length; ++i) {
const x = 26 - (s.charCodeAt(i - 1) - 'a'.charCodeAt(0));
ans += i * x;
}
return ans;
}
Use this to step through a reusable interview workflow for this problem.
Two nested loops check every pair or subarray. The outer loop fixes a starting point, the inner loop extends or searches. For n elements this gives up to n²/2 operations. No extra space, but the quadratic time is prohibitive for large inputs.
Most array problems have an O(n²) brute force (nested loops) and an O(n) optimal (single pass with clever state tracking). The key is identifying what information to maintain as you scan: a running max, a prefix sum, a hash map of seen values, or two pointers.
Review these before coding to avoid predictable interview regressions.
Wrong move: Loop endpoints miss first/last candidate.
Usually fails on: Fails on minimal arrays and exact-boundary answers.
Fix: Re-derive loops from inclusive/exclusive ranges before coding.