-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path3137-MinimumNumberOfOperationsToMakeWordKPeriodic.go
More file actions
85 lines (73 loc) · 2.78 KB
/
3137-MinimumNumberOfOperationsToMakeWordKPeriodic.go
File metadata and controls
85 lines (73 loc) · 2.78 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
package main
// 3137. Minimum Number of Operations to Make Word K-Periodic
// You are given a string word of size n, and an integer k such that k divides n.
// In one operation, you can pick any two indices i and j, that are divisible by k,
// then replace the substring of length k starting at i with the substring of length k starting at j.
// That is, replace the substring word[i..i + k - 1] with the substring word[j..j + k - 1].
// Return the minimum number of operations required to make word k-periodic.
// We say that word is k-periodic if there is some string s of length k such that word can be obtained by concatenating s an arbitrary number of times. F、
// or example, if word == “ababab”, then word is 2-periodic for s = "ab".
// Example 1:
// Input: word = "leetcodeleet", k = 4
// Output: 1
// Explanation:
// We can obtain a 4-periodic string by picking i = 4 and j = 0. After this operation, word becomes equal to "leetleetleet".
// Example 2:
// Input: word = "leetcoleet", k = 2
// Output: 3
// Explanation:
// We can obtain a 2-periodic string by applying the operations in the table below.
// i j word
// 0 2 etetcoleet
// 4 0 etetetleet
// 6 0 etetetetet
// Constraints:
// 1 <= n == word.length <= 10^5
// 1 <= k <= word.length
// k divides word.length.
// word consists only of lowercase English letters.
import "fmt"
func minimumOperationsToMakeKPeriodic(word string, k int) int {
mp, n := map[string]int{}, len(word)
for i := 0; i < n; i+= k {
mp[word[i:i+k]]++
}
res := n/k - 1
min := func (x, y int) int { if x < y { return x; }; return y; }
for _, v := range mp {
res = min(res, n/k - v)
}
return res
}
func minimumOperationsToMakeKPeriodic1(word string, k int) int {
n, mp := len(word), map[string]int{}
for i := 0; i < n; i += k {
mp[word[i:i+k]]++
}
mx := 0
max := func (x, y int) int { if x > y { return x; }; return y; }
for _, c := range mp {
mx = max(mx, c)
}
return n/k - mx
}
func main() {
// Example 1:
// Input: word = "leetcodeleet", k = 4
// Output: 1
// Explanation:
// We can obtain a 4-periodic string by picking i = 4 and j = 0. After this operation, word becomes equal to "leetleetleet".
fmt.Println(minimumOperationsToMakeKPeriodic("leetcodeleet", 4)) // 1
// Example 2:
// Input: word = "leetcoleet", k = 2
// Output: 3
// Explanation:
// We can obtain a 2-periodic string by applying the operations in the table below.
// i j word
// 0 2 etetcoleet
// 4 0 etetetleet
// 6 0 etetetetet
fmt.Println(minimumOperationsToMakeKPeriodic("leetcoleet", 2)) // 3
fmt.Println(minimumOperationsToMakeKPeriodic1("leetcodeleet", 4)) // 1
fmt.Println(minimumOperationsToMakeKPeriodic1("leetcoleet", 2)) // 3
}