From 8db6239b8d06b5100d4a22724c746c5c02f8a281 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?L=C3=BD=20Xu=C3=A2n=20Sang?= Date: Sun, 20 Sep 2026 08:42:38 +0700 Subject: [PATCH] add 3498 solution --- .../description.md | 42 +++++++ .../solution.md | 112 ++++++++++++++++++ README.md | 5 +- SUMMARY.md | 1 + _sidebar.md | 1 + 5 files changed, 159 insertions(+), 2 deletions(-) create mode 100644 Easy/3498.Reverse-Degree-of-a-String/description.md create mode 100644 Easy/3498.Reverse-Degree-of-a-String/solution.md diff --git a/Easy/3498.Reverse-Degree-of-a-String/description.md b/Easy/3498.Reverse-Degree-of-a-String/description.md new file mode 100644 index 0000000..0e008bd --- /dev/null +++ b/Easy/3498.Reverse-Degree-of-a-String/description.md @@ -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. diff --git a/Easy/3498.Reverse-Degree-of-a-String/solution.md b/Easy/3498.Reverse-Degree-of-a-String/solution.md new file mode 100644 index 0000000..9720c45 --- /dev/null +++ b/Easy/3498.Reverse-Degree-of-a-String/solution.md @@ -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. diff --git a/README.md b/README.md index c7b5b08..a8d1429 100644 --- a/README.md +++ b/README.md @@ -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-.md`). -### Easy (58) +### Easy (59) | Problem | LeetCode | Solution | | ------------------------------------------------------------------ | --------------------------------------------------------------------------------------------------- | ----------------------------------------------------------------------------------------------------------------------------------------- | @@ -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) | diff --git a/SUMMARY.md b/SUMMARY.md index b9b9097..f12ee67 100644 --- a/SUMMARY.md +++ b/SUMMARY.md @@ -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) diff --git a/_sidebar.md b/_sidebar.md index 97b249c..5bea782 100644 --- a/_sidebar.md +++ b/_sidebar.md @@ -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)