-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path3307-FindTheKthCharacterInStringGameII.go
More file actions
114 lines (100 loc) · 4.2 KB
/
3307-FindTheKthCharacterInStringGameII.go
File metadata and controls
114 lines (100 loc) · 4.2 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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
package main
// 3307. Find the K-th Character in String Game II
// Alice and Bob are playing a game. Initially, Alice has a string word = "a".
// You are given a positive integer k.
// You are also given an integer array operations, where operations[i] represents the type of the ith operation.
// Now Bob will ask Alice to perform all operations in sequence:
// 1. If operations[i] == 0, append a copy of word to itself.
// 2. If operations[i] == 1, generate a new string by changing each character in word to its next character in the English alphabet,
// and append it to the original word.
// For example, performing the operation on "c" generates "cd" and performing the operation on "zb" generates "zbac".
// Return the value of the kth character in word after performing all the operations.
// Note that the character 'z' can be changed to 'a' in the second type of operation.
// Example 1:
// Input: k = 5, operations = [0,0,0]
// Output: "a"
// Explanation:
// Initially, word == "a". Alice performs the three operations as follows:
// Appends "a" to "a", word becomes "aa".
// Appends "aa" to "aa", word becomes "aaaa".
// Appends "aaaa" to "aaaa", word becomes "aaaaaaaa".
// Example 2:
// Input: k = 10, operations = [0,1,0,1]
// Output: "b"
// Explanation:
// Initially, word == "a". Alice performs the four operations as follows:
// Appends "a" to "a", word becomes "aa".
// Appends "bb" to "aa", word becomes "aabb".
// Appends "aabb" to "aabb", word becomes "aabbaabb".
// Appends "bbccbbcc" to "aabbaabb", word becomes "aabbaabbbbccbbcc".
// Constraints:
// 1 <= k <= 10^14
// 1 <= operations.length <= 100
// operations[i] is either 0 or 1.
// The input is generated such that word has at least k characters after all operations.
import "fmt"
func kthCharacter(k int64, operations []int) byte {
k--
c := 0
for i := len(operations) - 1; i >= 0; i-- {
if k >> i & 1 == 1 {
c += operations[i]
}
}
return 'a' + byte(c % 26)
}
func kthCharacter1(k int64, operations []int) byte {
// 0:复制一份
// 1:+1复制一份
incr, e, length := 0, 0, int64(1)
for length < k {
e++
length *= 2
}
// length = 2^e
index := e - 1
for index >= 0 {
if k > length / 2 { // 可能发生变化
if operations[index] == 1 {
incr++
}
k = k - length / 2
}
length /= 2
index--
}
return 'a' + byte(incr % 26)
}
func main() {
// Example 1:
// Input: k = 5, operations = [0,0,0]
// Output: "a"
// Explanation:
// Initially, word == "a". Alice performs the three operations as follows:
// Appends "a" to "a", word becomes "aa".
// Appends "aa" to "aa", word becomes "aaaa".
// Appends "aaaa" to "aaaa", word becomes "aaaaaaaa".
fmt.Printf("%c\n",kthCharacter(5,[]int{0,0,0})) // "a"
// Example 2:
// Input: k = 10, operations = [0,1,0,1]
// Output: "b"
// Explanation:
// Initially, word == "a". Alice performs the four operations as follows:
// Appends "a" to "a", word becomes "aa".
// Appends "bb" to "aa", word becomes "aabb".
// Appends "aabb" to "aabb", word becomes "aabbaabb".
// Appends "bbccbbcc" to "aabbaabb", word becomes "aabbaabbbbccbbcc".
fmt.Printf("%c\n",kthCharacter(10, []int{0,1,0,1})) // "b"
fmt.Printf("%c\n",kthCharacter(10, []int{1,0,1,0,1,0,1,0,1,0})) // "b"
fmt.Printf("%c\n",kthCharacter(10, []int{0,0,0,0,0,0,0,0,0,0})) // "a"
fmt.Printf("%c\n",kthCharacter(10, []int{1,1,1,1,1,1,1,1,1,1})) // "c"
fmt.Printf("%c\n",kthCharacter(10, []int{1,1,1,1,1,0,0,0,0,0})) // "c"
fmt.Printf("%c\n",kthCharacter(10, []int{0,0,0,0,0,1,1,1,1,1})) // "a"
fmt.Printf("%c\n",kthCharacter1(5,[]int{0,0,0})) // "a"
fmt.Printf("%c\n",kthCharacter1(10, []int{0,1,0,1})) // "b"
fmt.Printf("%c\n",kthCharacter1(10, []int{1,0,1,0,1,0,1,0,1,0})) // "b"
fmt.Printf("%c\n",kthCharacter1(10, []int{0,0,0,0,0,0,0,0,0,0})) // "a"
fmt.Printf("%c\n",kthCharacter1(10, []int{1,1,1,1,1,1,1,1,1,1})) // "c"
fmt.Printf("%c\n",kthCharacter1(10, []int{1,1,1,1,1,0,0,0,0,0})) // "c"
fmt.Printf("%c\n",kthCharacter1(10, []int{0,0,0,0,0,1,1,1,1,1})) // "a"
}