-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path3029-MinimumTimeToRevertWordToInitialStateI.go
More file actions
101 lines (87 loc) · 4.63 KB
/
3029-MinimumTimeToRevertWordToInitialStateI.go
File metadata and controls
101 lines (87 loc) · 4.63 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
package main
// 3029. Minimum Time to Revert Word to Initial State I
// You are given a 0-indexed string word and an integer k.
// At every second, you must perform the following operations:
// 1. Remove the first k characters of word.
// 2. Add any k characters to the end of word.
// Note that you do not necessarily need to add the same characters that you removed.
// However, you must perform both operations at every second.
// Return the minimum time greater than zero required for word to revert to its initial state.
// Example 1:
// Input: word = "abacaba", k = 3
// Output: 2
// Explanation: At the 1st second, we remove characters "aba" from the prefix of word, and add characters "bac" to the end of word. Thus, word becomes equal to "cababac".
// At the 2nd second, we remove characters "cab" from the prefix of word, and add "aba" to the end of word. Thus, word becomes equal to "abacaba" and reverts to its initial state.
// It can be shown that 2 seconds is the minimum time greater than zero required for word to revert to its initial state.
// Example 2:
// Input: word = "abacaba", k = 4
// Output: 1
// Explanation: At the 1st second, we remove characters "abac" from the prefix of word, and add characters "caba" to the end of word. Thus, word becomes equal to "abacaba" and reverts to its initial state.
// It can be shown that 1 second is the minimum time greater than zero required for word to revert to its initial state.
// Example 3:
// Input: word = "abcbabcd", k = 2
// Output: 4
// Explanation: At every second, we will remove the first 2 characters of word, and add the same characters to the end of word.
// After 4 seconds, word becomes equal to "abcbabcd" and reverts to its initial state.
// It can be shown that 4 seconds is the minimum time greater than zero required for word to revert to its initial state.
// Constraints:
// 1 <= word.length <= 50
// 1 <= k <= word.length
// word consists only of lowercase English letters.
import "fmt"
import "strings"
func minimumTimeToInitialState(word string, k int) int {
res, check := 0, word
for len(check) >= k {
res++
check = check[k:]
if check == word[:len(check)] {
if len(word[len(check):]) % k == 0 {
return res
}
return res + 1
}
}
return res + 1
}
func minimumTimeToInitialState1(word string, k int) int {
res, arr := 0, []byte(word)
min := func (x, y int) int { if x < y { return x; }; return y; }
for len(arr) > 0 {
res++
arr = arr[min(k, len(arr)):]
if strings.HasPrefix(word, string(arr)) {
break
}
}
return res
}
func main() {
// Example 1:
// Input: word = "abacaba", k = 3
// Output: 2
// Explanation: At the 1st second, we remove characters "aba" from the prefix of word, and add characters "bac" to the end of word. Thus, word becomes equal to "cababac".
// At the 2nd second, we remove characters "cab" from the prefix of word, and add "aba" to the end of word. Thus, word becomes equal to "abacaba" and reverts to its initial state.
// It can be shown that 2 seconds is the minimum time greater than zero required for word to revert to its initial state.
fmt.Println(minimumTimeToInitialState("abacaba", 3)) // 2
// Example 2:
// Input: word = "abacaba", k = 4
// Output: 1
// Explanation: At the 1st second, we remove characters "abac" from the prefix of word, and add characters "caba" to the end of word. Thus, word becomes equal to "abacaba" and reverts to its initial state.
// It can be shown that 1 second is the minimum time greater than zero required for word to revert to its initial state.
fmt.Println(minimumTimeToInitialState("abacaba", 4)) // 1
// Example 3:
// Input: word = "abcbabcd", k = 2
// Output: 4
// Explanation: At every second, we will remove the first 2 characters of word, and add the same characters to the end of word.
// After 4 seconds, word becomes equal to "abcbabcd" and reverts to its initial state.
// It can be shown that 4 seconds is the minimum time greater than zero required for word to revert to its initial state.
fmt.Println(minimumTimeToInitialState("abcbabcd", 2)) // 4
fmt.Println(minimumTimeToInitialState("bluefrog", 2)) // 4
fmt.Println(minimumTimeToInitialState("leetcode", 2)) // 4
fmt.Println(minimumTimeToInitialState1("abacaba", 3)) // 2
fmt.Println(minimumTimeToInitialState1("abacaba", 4)) // 1
fmt.Println(minimumTimeToInitialState1("abcbabcd", 2)) // 4
fmt.Println(minimumTimeToInitialState1("bluefrog", 2)) // 4
fmt.Println(minimumTimeToInitialState1("leetcode", 2)) // 4
}