|
| 1 | +--- |
| 2 | +comments: true |
| 3 | +difficulty: Easy |
| 4 | +edit_url: https://github.com/doocs/leetcode/edit/main/solution/3400-3499/3498.Reverse%20Degree%20of%20a%20String/README_EN.md |
| 5 | +--- |
| 6 | + |
| 7 | +<!-- problem:start --> |
| 8 | + |
| 9 | +# [3498. Reverse Degree of a String](https://leetcode.com/problems/reverse-degree-of-a-string) |
| 10 | + |
| 11 | +[中文文档](/solution/3400-3499/3498.Reverse%20Degree%20of%20a%20String/README.md) |
| 12 | + |
| 13 | +## Description |
| 14 | + |
| 15 | +<!-- description:start --> |
| 16 | + |
| 17 | +<p>Given a string <code>s</code>, calculate its <strong>reverse degree</strong>.</p> |
| 18 | + |
| 19 | +<p>The <strong>reverse degree</strong> is calculated as follows:</p> |
| 20 | + |
| 21 | +<ol> |
| 22 | + <li>For each character, multiply its position in the <em>reversed</em> alphabet (<code>'a'</code> = 26, <code>'b'</code> = 25, ..., <code>'z'</code> = 1) with its position in the string <strong>(1-indexed)</strong>.</li> |
| 23 | + <li>Sum these products for all characters in the string.</li> |
| 24 | +</ol> |
| 25 | + |
| 26 | +<p>Return the <strong>reverse degree</strong> of <code>s</code>.</p> |
| 27 | + |
| 28 | +<p> </p> |
| 29 | +<p><strong class="example">Example 1:</strong></p> |
| 30 | + |
| 31 | +<div class="example-block"> |
| 32 | +<p><strong>Input:</strong> <span class="example-io">s = "abc"</span></p> |
| 33 | + |
| 34 | +<p><strong>Output:</strong> <span class="example-io">148</span></p> |
| 35 | + |
| 36 | +<p><strong>Explanation:</strong></p> |
| 37 | + |
| 38 | +<table style="border: 1px solid black;"> |
| 39 | + <tbody> |
| 40 | + <tr> |
| 41 | + <th style="border: 1px solid black;">Letter</th> |
| 42 | + <th style="border: 1px solid black;">Index in Reversed Alphabet</th> |
| 43 | + <th style="border: 1px solid black;">Index in String</th> |
| 44 | + <th style="border: 1px solid black;">Product</th> |
| 45 | + </tr> |
| 46 | + <tr> |
| 47 | + <td style="border: 1px solid black;"><code>'a'</code></td> |
| 48 | + <td style="border: 1px solid black;">26</td> |
| 49 | + <td style="border: 1px solid black;">1</td> |
| 50 | + <td style="border: 1px solid black;">26</td> |
| 51 | + </tr> |
| 52 | + <tr> |
| 53 | + <td style="border: 1px solid black;"><code>'b'</code></td> |
| 54 | + <td style="border: 1px solid black;">25</td> |
| 55 | + <td style="border: 1px solid black;">2</td> |
| 56 | + <td style="border: 1px solid black;">50</td> |
| 57 | + </tr> |
| 58 | + <tr> |
| 59 | + <td style="border: 1px solid black;"><code>'c'</code></td> |
| 60 | + <td style="border: 1px solid black;">24</td> |
| 61 | + <td style="border: 1px solid black;">3</td> |
| 62 | + <td style="border: 1px solid black;">72</td> |
| 63 | + </tr> |
| 64 | + </tbody> |
| 65 | +</table> |
| 66 | + |
| 67 | +<p>The reversed degree is <code>26 + 50 + 72 = 148</code>.</p> |
| 68 | +</div> |
| 69 | + |
| 70 | +<p><strong class="example">Example 2:</strong></p> |
| 71 | + |
| 72 | +<div class="example-block"> |
| 73 | +<p><strong>Input:</strong> <span class="example-io">s = "zaza"</span></p> |
| 74 | + |
| 75 | +<p><strong>Output:</strong> <span class="example-io">160</span></p> |
| 76 | + |
| 77 | +<p><strong>Explanation:</strong></p> |
| 78 | + |
| 79 | +<table style="border: 1px solid black;"> |
| 80 | + <tbody> |
| 81 | + <tr> |
| 82 | + <th style="border: 1px solid black;">Letter</th> |
| 83 | + <th style="border: 1px solid black;">Index in Reversed Alphabet</th> |
| 84 | + <th style="border: 1px solid black;">Index in String</th> |
| 85 | + <th style="border: 1px solid black;">Product</th> |
| 86 | + </tr> |
| 87 | + <tr> |
| 88 | + <td style="border: 1px solid black;"><code>'z'</code></td> |
| 89 | + <td style="border: 1px solid black;">1</td> |
| 90 | + <td style="border: 1px solid black;">1</td> |
| 91 | + <td style="border: 1px solid black;">1</td> |
| 92 | + </tr> |
| 93 | + <tr> |
| 94 | + <td style="border: 1px solid black;"><code>'a'</code></td> |
| 95 | + <td style="border: 1px solid black;">26</td> |
| 96 | + <td style="border: 1px solid black;">2</td> |
| 97 | + <td style="border: 1px solid black;">52</td> |
| 98 | + </tr> |
| 99 | + <tr> |
| 100 | + <td style="border: 1px solid black;"><code>'z'</code></td> |
| 101 | + <td style="border: 1px solid black;">1</td> |
| 102 | + <td style="border: 1px solid black;">3</td> |
| 103 | + <td style="border: 1px solid black;">3</td> |
| 104 | + </tr> |
| 105 | + <tr> |
| 106 | + <td style="border: 1px solid black;"><code>'a'</code></td> |
| 107 | + <td style="border: 1px solid black;">26</td> |
| 108 | + <td style="border: 1px solid black;">4</td> |
| 109 | + <td style="border: 1px solid black;">104</td> |
| 110 | + </tr> |
| 111 | + </tbody> |
| 112 | +</table> |
| 113 | + |
| 114 | +<p>The reverse degree is <code>1 + 52 + 3 + 104 = 160</code>.</p> |
| 115 | +</div> |
| 116 | + |
| 117 | +<p> </p> |
| 118 | +<p><strong>Constraints:</strong></p> |
| 119 | + |
| 120 | +<ul> |
| 121 | + <li><code>1 <= s.length <= 1000</code></li> |
| 122 | + <li><code>s</code> contains only lowercase English letters.</li> |
| 123 | +</ul> |
| 124 | + |
| 125 | +<!-- description:end --> |
| 126 | + |
| 127 | +## Solutions |
| 128 | + |
| 129 | +<!-- solution:start --> |
| 130 | + |
| 131 | +### Solution 1 |
| 132 | + |
| 133 | +<!-- tabs:start --> |
| 134 | + |
| 135 | +#### Python3 |
| 136 | + |
| 137 | +```python |
| 138 | + |
| 139 | +``` |
| 140 | + |
| 141 | +#### Java |
| 142 | + |
| 143 | +```java |
| 144 | + |
| 145 | +``` |
| 146 | + |
| 147 | +#### C++ |
| 148 | + |
| 149 | +```cpp |
| 150 | + |
| 151 | +``` |
| 152 | + |
| 153 | +#### Go |
| 154 | + |
| 155 | +```go |
| 156 | + |
| 157 | +``` |
| 158 | + |
| 159 | +<!-- tabs:end --> |
| 160 | + |
| 161 | +<!-- solution:end --> |
| 162 | + |
| 163 | +<!-- problem:end --> |
0 commit comments