-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path3498-ReverseDegreeOfAString.go
More file actions
73 lines (63 loc) · 1.85 KB
/
3498-ReverseDegreeOfAString.go
File metadata and controls
73 lines (63 loc) · 1.85 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
package main
// 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.
import "fmt"
func reverseDegree(s string) int {
res := 0
for i, v := range s {
// fmt.Println(v, " ", (26 - int(v - 'a')))
res += (i + 1) * (26 - int(v - 'a'))
}
return res
}
func main() {
// 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.
fmt.Println(reverseDegree("abc")) // 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.
fmt.Println(reverseDegree("zaza")) // 160
fmt.Println(reverseDegree("bluefrog")) // 564
fmt.Println(reverseDegree("leetcode")) // 682
}