Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 42 additions & 0 deletions Easy/3498.Reverse-Degree-of-a-String/description.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
# 3498. Reverse Degree of a String

Given a string `s`, calculate its **reverse degree**.

The **reverse degree** is calculated as follows:

1. For each character, multiply its position in the *reversed* alphabet
(`'a' = 26`, `'b' = 25`, ..., `'z' = 1`) by its position in the string
**(1-indexed)**.
2. Sum these products for all characters in the string.

Return the **reverse degree** of `s`.

## Example 1

```text
Input: s = "abc"
Output: 148
Explanation:
'a': 26 * 1 = 26
'b': 25 * 2 = 50
'c': 24 * 3 = 72
The reverse degree is 26 + 50 + 72 = 148.
```

## Example 2

```text
Input: s = "zaza"
Output: 160
Explanation:
'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 <= 1000`
- `s` contains only lowercase English letters.
112 changes: 112 additions & 0 deletions Easy/3498.Reverse-Degree-of-a-String/solution.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
# 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

1. Traverse `s` from left to right while tracking the 0-indexed position `i`.
2. Compute the reversed-alphabet value as `'z' - ch + 1`.
3. Multiply that value by `i + 1` and add it to the running sum.
4. 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 `n` is the length of `s` — 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

```go
func reverseDegree(s string) int {
ans := 0
for index, ch := range s {
ans += (index + 1) * (int('z' - ch) + 1)
}
return ans
}
```

## Rust

```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

```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.
5 changes: 3 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,11 @@ Easy/350.Intersection-of-Two-Arrays-II/

## Solutions index

Total: **219** problems with at least one solution file.
Total: **220** problems with at least one solution file.

Solution links use variant names when multiple approaches or languages exist (`main` = `solution.md`, others = `solution-<variant>.md`).

### Easy (58)
### Easy (59)

| Problem | LeetCode | Solution |
| ------------------------------------------------------------------ | --------------------------------------------------------------------------------------------------- | ----------------------------------------------------------------------------------------------------------------------------------------- |
Expand Down Expand Up @@ -75,6 +75,7 @@ Solution links use variant names when multiple approaches or languages exist (`m
| 3375. Minimum Operations to Make Array Values Equal to k | [Link](https://leetcode.com/problems/minimum-operations-to-make-array-values-equal-to-k/) | [main](Easy/3375.Minimum-Operations-to-Make-Array-Values-Equal-to-k/solution.md) |
| 3471. Find the Largest Almost Missing Integer | [Link](https://leetcode.com/problems/find-the-largest-almost-missing-integer/) | [main](Easy/3471.Find-the-Largest-Almost-Missing-Integer/solution.md) |
| 3483. Unique 3-Digit Even Numbers | [Link](https://leetcode.com/problems/unique-3-digit-even-numbers/) | [main](Easy/3483.Unique-3-Digit-Even-Numbers/solution.md) |
| 3498. Reverse Degree of a String | [Link](https://leetcode.com/problems/reverse-degree-of-a-string/) | [main](Easy/3498.Reverse-Degree-of-a-String/solution.md) |
| 3536. Maximum Product of Two Digits | [Link](https://leetcode.com/problems/maximum-product-of-two-digits/) | [main](Easy/3536.Maximum-Product-of-Two-Digits/solution.md) |
| 3622. Check Divisibility by Digit Sum and Product | [Link](https://leetcode.com/problems/check-divisibility-by-digit-sum-and-product/) | [main](Easy/3622.Check-Divisibility-by-Digit-Sum-and-Product/solution.md) |
| 3637. Trionic Array I | [Link](https://leetcode.com/problems/trionic-array-i/) | [main](Easy/3637.Trionic-Array-I/solution.md) |
Expand Down
1 change: 1 addition & 0 deletions SUMMARY.md
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@
* [3375. Minimum Operations to Make Array Values Equal to k](Easy/3375.Minimum-Operations-to-Make-Array-Values-Equal-to-k/solution.md)
* [3471. Find the Largest Almost Missing Integer](Easy/3471.Find-the-Largest-Almost-Missing-Integer/solution.md)
* [3483. Unique 3 Digit Even Numbers](Easy/3483.Unique-3-Digit-Even-Numbers/solution.md)
* [3498. Reverse Degree of a String](Easy/3498.Reverse-Degree-of-a-String/solution.md)
* [3536. Maximum Product of Two Digits](Easy/3536.Maximum-Product-of-Two-Digits/solution.md)
* [3622. Check Divisibility by Digit Sum and Product](Easy/3622.Check-Divisibility-by-Digit-Sum-and-Product/solution.md)
* [3637. Trionic Array I](Easy/3637.Trionic-Array-I/solution.md)
Expand Down
1 change: 1 addition & 0 deletions _sidebar.md
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@
- [3375. Minimum Operations to Make Array Values Equal to k](Easy/3375.Minimum-Operations-to-Make-Array-Values-Equal-to-k/solution.md)
- [3471. Find the Largest Almost Missing Integer](Easy/3471.Find-the-Largest-Almost-Missing-Integer/solution.md)
- [3483. Unique 3 Digit Even Numbers](Easy/3483.Unique-3-Digit-Even-Numbers/solution.md)
- [3498. Reverse Degree of a String](Easy/3498.Reverse-Degree-of-a-String/solution.md)
- [3536. Maximum Product of Two Digits](Easy/3536.Maximum-Product-of-Two-Digits/solution.md)
- [3622. Check Divisibility by Digit Sum and Product](Easy/3622.Check-Divisibility-by-Digit-Sum-and-Product/solution.md)
- [3637. Trionic Array I](Easy/3637.Trionic-Array-I/solution.md)
Expand Down
Loading