-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
65 lines (52 loc) · 1.01 KB
/
main.go
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
package dnquestions
// RepeatedString returns the number of times the letter 'a' appears in the string
// assuming that `str` repeated `n` times.
func RepeatedString(str string, n int) int {
strLen := len(str)
if strLen == 0 {
return 0
}
count := 0
for _, char := range str {
if char == 'a' {
count++
}
}
count *= n / strLen
for _, val := range str[:n%strLen] {
if val == 'a' {
count++
}
}
return count
}
// MinimumRemovals returns the minimum number of removals needed to make the string
// alternating between 'A' and 'B'.
func MinimumRemovals(str string) int {
count := 0
for i := range str {
if i == len(str)-1 {
break
}
if str[i] == str[i+1] {
count++
i++
}
}
return count
}
// MinSwapsToSort returns the minimum number of swaps needed to sort the array.
func MinSwapsToSort(arr []int) int {
swaps := 0
for i := 0; i < len(arr); i++ {
if arr[i] == i+1 {
continue
}
temp := arr[i]
arr[i] = arr[temp-1]
arr[temp-1] = temp
swaps++
i--
}
return swaps
}