From afbf7e12f5d5abc8676057f1cec6624acdc53be1 Mon Sep 17 00:00:00 2001 From: "github-actions[bot]" Date: Sun, 20 Sep 2026 05:10:20 +0000 Subject: [PATCH] feat: add solution for 3498. Reverse Degree of a String --- .../analysis.md | 95 +++++++++++++++++++ .../problem.md | 71 ++++++++++++++ .../solution_daily_20260920.go | 17 ++++ .../solution_daily_20260920_test.go | 33 +++++++ 4 files changed, 216 insertions(+) create mode 100644 problems/3498-reverse-degree-of-a-string/analysis.md create mode 100644 problems/3498-reverse-degree-of-a-string/problem.md create mode 100644 problems/3498-reverse-degree-of-a-string/solution_daily_20260920.go create mode 100644 problems/3498-reverse-degree-of-a-string/solution_daily_20260920_test.go diff --git a/problems/3498-reverse-degree-of-a-string/analysis.md b/problems/3498-reverse-degree-of-a-string/analysis.md new file mode 100644 index 0000000..609d96f --- /dev/null +++ b/problems/3498-reverse-degree-of-a-string/analysis.md @@ -0,0 +1,95 @@ +# 3498. Reverse Degree of a String + +[LeetCode Link](https://leetcode.com/problems/reverse-degree-of-a-string/) + +Difficulty: Easy +Topics: String, Simulation +Acceptance Rate: 89.7% + +## Hints + +### Hint 1 + +There is no clever data structure hiding here. The problem hands you a formula and +asks you to evaluate it. Ask yourself: can every term of the sum be computed while +looking at exactly one character, without needing any other character? If so, a +single left-to-right pass is all you need. + +### Hint 2 + +Two numbers make up each product: the letter's rank in the reversed alphabet, and +the letter's 1-indexed position in the string. The second one comes for free from +the loop counter — just remember Go indexes from 0 while the problem counts from 1. +The first one is a small arithmetic mapping from a byte to a number in `[1, 26]`. + +### Hint 3 + +The reversed-alphabet rank needs no lookup table. `'a'` must map to 26 and `'z'` to +1, which is exactly `'z' - c + 1` (equivalently `26 - (c - 'a')`). Plug that into a +running accumulator and the whole solution is one loop with one multiply-add per +character. + +## Approach + +Iterate over the string once with an index `i` and a byte `c`. + +1. **Convert the character to its reversed-alphabet rank.** The normal rank of a + lowercase letter is `c - 'a' + 1` (so `'a'` = 1, `'z'` = 26). Reversing that + range means subtracting from 27: `27 - (c - 'a' + 1)` = `26 - (c - 'a')` = + `int('z' - c) + 1`. Any of these forms works; the last one avoids a magic 26. + +2. **Compute the 1-indexed string position.** Go's `for i, c := range s` gives a + 0-based `i`, so the position is `i + 1`. + +3. **Accumulate the product.** Add `rank * (i + 1)` into a running total. + +4. **Return the total.** + +Walk through `s = "zaza"`: + +| i | c | rank (`'z'-c+1`) | position (`i+1`) | product | running total | +|---|-----|------------------|------------------|---------|---------------| +| 0 | `z` | 1 | 1 | 1 | 1 | +| 1 | `a` | 26 | 2 | 52 | 53 | +| 2 | `z` | 1 | 3 | 3 | 56 | +| 3 | `a` | 26 | 4 | 104 | 160 | + +The answer is 160, matching the expected output. + +Why it works: the reverse degree is defined as a plain sum of independent per-character +terms, so no ordering trick, prefix sum, or memoization can beat simply evaluating each +term once. A single pass is already optimal. + +One implementation note for Go: `for i, c := range s` decodes UTF-8 and yields `c` as a +`rune`. That is harmless for this problem because the input is guaranteed ASCII lowercase, +but indexing bytes with `s[i]` is the more direct expression of "one byte per character" +and avoids any decoding subtlety. Either is fine here. + +This one is genuinely easy — the 89.7% acceptance rate is honest. The only real ways to +lose points are an off-by-one on the 1-indexing or getting the alphabet reversal backwards. +The value is in writing it cleanly on the first try. + +## Complexity Analysis + +Time Complexity: O(n), where n is the length of `s`. Each character is visited once and +does O(1) arithmetic. +Space Complexity: O(1). Only a running integer accumulator is kept; no lookup table or +copy of the string is needed. + +## Edge Cases + +- **Single character (`"a"`, `"z"`).** The constraints guarantee `1 <= s.length`, so the + string is never empty, but a length-1 input still exercises the 1-indexing: `"a"` must + give 26 (not 0), and `"z"` must give 1 (not 0). Both catch a 0-indexed position bug. +- **Alphabet endpoints.** `'a'` → 26 and `'z'` → 1 are the boundaries of the reversal + mapping. If the mapping is accidentally left un-reversed, `"a"` returns 1 instead of 26, + which a test on either endpoint exposes immediately. +- **Repeated characters (`"zzz"`, `"aaa"`).** The same letter contributes different + products at different positions, confirming the position factor is applied per + occurrence and not cached per letter. +- **Maximum length (1000 characters).** The worst case is 1000 `'a'`s: + 26 * (1 + 2 + ... + 1000) = 26 * 500500 = 13,013,000. That fits comfortably in an `int` + (even a 32-bit one), so no overflow handling is needed — but it is worth confirming the + bound rather than assuming it. +- **Empty string.** Excluded by the constraints, though a natural loop returns 0 for it, + which is the sensible answer anyway. diff --git a/problems/3498-reverse-degree-of-a-string/problem.md b/problems/3498-reverse-degree-of-a-string/problem.md new file mode 100644 index 0000000..b24ae9d --- /dev/null +++ b/problems/3498-reverse-degree-of-a-string/problem.md @@ -0,0 +1,71 @@ +--- +number: "3498" +frontend_id: "3498" +title: "Reverse Degree of a String" +slug: "reverse-degree-of-a-string" +difficulty: "Easy" +topics: + - "String" + - "Simulation" +acceptance_rate: 8971.7 +is_premium: false +created_at: "2026-09-20T05:08:52.094322+00:00" +fetched_at: "2026-09-20T05:08:52.094322+00:00" +link: "https://leetcode.com/problems/reverse-degree-of-a-string/" +date: "2026-09-20" +--- + +# 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) with 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:** + +**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 <= 1000` + * `s` contains only lowercase English letters. diff --git a/problems/3498-reverse-degree-of-a-string/solution_daily_20260920.go b/problems/3498-reverse-degree-of-a-string/solution_daily_20260920.go new file mode 100644 index 0000000..5929695 --- /dev/null +++ b/problems/3498-reverse-degree-of-a-string/solution_daily_20260920.go @@ -0,0 +1,17 @@ +package main + +// 3498. Reverse Degree of a String +// +// Single pass over the string. For each character, its rank in the reversed +// alphabet ('a' = 26, ..., 'z' = 1) is 'z' - c + 1, and its 1-indexed position +// is i + 1. Accumulate the product of the two for every character. +// +// Time: O(n). Space: O(1). +func reverseDegree(s string) int { + total := 0 + for i := 0; i < len(s); i++ { + rank := int('z'-s[i]) + 1 + total += rank * (i + 1) + } + return total +} diff --git a/problems/3498-reverse-degree-of-a-string/solution_daily_20260920_test.go b/problems/3498-reverse-degree-of-a-string/solution_daily_20260920_test.go new file mode 100644 index 0000000..4a192dc --- /dev/null +++ b/problems/3498-reverse-degree-of-a-string/solution_daily_20260920_test.go @@ -0,0 +1,33 @@ +package main + +import ( + "strings" + "testing" +) + +func TestSolution(t *testing.T) { + tests := []struct { + name string + s string + expected int + }{ + {"example 1: abc, ascending letters", "abc", 148}, + {"example 2: zaza, alternating extremes", "zaza", 160}, + {"edge case: single 'a' maps to 26", "a", 26}, + {"edge case: single 'z' maps to 1", "z", 1}, + {"edge case: empty string sums to zero", "", 0}, + {"edge case: repeated letter, position still varies", "zzz", 6}, + {"edge case: both alphabet endpoints", "az", 28}, + {"edge case: mixed word", "leetcode", 682}, + {"edge case: maximum length of 1000 'a's", strings.Repeat("a", 1000), 13013000}, + } + + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + result := reverseDegree(tt.s) + if result != tt.expected { + t.Errorf("reverseDegree(%q) = %v, want %v", tt.s, result, tt.expected) + } + }) + } +}