-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsolution2325.go
More file actions
42 lines (38 loc) · 974 Bytes
/
solution2325.go
File metadata and controls
42 lines (38 loc) · 974 Bytes
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
package solution2325
// ============================================================================
// 2325. Decode the Message
// URL: https://leetcode.com/problems/decode-the-message/
// ============================================================================
/*
$ go test -bench=. -benchmem
goos: linux
goarch: amd64
pkg: GoLeetCode/solutions/2325---Decode-the-Message
cpu: 13th Gen Intel(R) Core(TM) i7-13700K
Benchmark_decodeMessage-24 1101666 1182 ns/op 290 B/op 3 allocs/op
PASS
*/
func decodeMessage(key string, message string) string {
m := make(map[byte]byte, 26)
idx := 0
for _, char := range key {
if char != ' ' {
ch := byte(char)
_, ok := m[ch]
if !ok {
m[ch] = byte(idx + 'a')
idx++
}
}
}
ans := make([]byte, 0, len(message))
for _, char := range message {
if char == ' ' {
ans = append(ans, ' ')
} else {
ch := byte(char)
ans = append(ans, m[ch])
}
}
return string(ans)
}