-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
554 lines (461 loc) · 11.3 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
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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
package GoCodingTraining
import (
"container/list"
"math"
"reflect"
"sort"
"strconv"
"strings"
"unicode"
)
func reverseString(input string) string {
var returnValue string
for _, value := range input {
returnValue = string(value) + returnValue
}
return returnValue
}
func delChar(s []rune, index int) []rune {
return append(s[0:index], s[index+1:]...)
}
func hasBit(n int, pos uint) bool {
val := n & (1 << pos)
return val > 0
}
func setBit(n int, pos uint) int {
n |= 1 << pos
return n
}
func clearBit(n int, pos uint) int {
mask := ^(1 << pos)
n &= mask
return n
}
type sortableInt32array []int32
func (f sortableInt32array) Len() int {
return len(f)
}
func (f sortableInt32array) Less(i, j int) bool {
return f[i] < f[j]
}
func (f sortableInt32array) Swap(i, j int) {
f[i], f[j] = f[j], f[i]
}
func min(a, b int32) int32 {
if a < b {
return a
}
return b
}
func pickingNumbers(a []int32) int32 {
// Write your code here
return 0
}
func pageCount(n int32, p int32) int32 {
pagesInBook := p / 2
totalPages := n / 2
fromFront := pagesInBook
fromBack := totalPages - pagesInBook
return min(fromBack, fromFront)
}
func twoArrays(k int32, A []int32, B []int32) string {
var sortableA sortableInt32array = A
var sortableB sortableInt32array = B
sort.Sort(sortableA)
sort.Sort(sort.Reverse(sortableB))
for i := range sortableA {
if !(sortableA[i]+sortableB[i] >= k) {
return "NO"
}
}
return "YES"
}
func maximumPerimeterTriangle(sticks []int32) []int32 {
maxPerimeter := int64(0)
var returnArray []int32
sort.Slice(sticks, func(i, j int) bool { return sticks[i] < sticks[j] })
for i, j := 0, len(sticks)-1; i < j; i, j = i+1, j-1 {
sticks[i], sticks[j] = sticks[j], sticks[i]
}
for i := 0; i < len(sticks)-2; i++ {
if (sticks[i]+sticks[i+1] > sticks[i+2]) && (sticks[i]+sticks[i+2] > sticks[i+1]) && (sticks[i+1]+sticks[i+2] > sticks[i]) {
if int64(sticks[i])+int64(sticks[i+1])+int64(sticks[i+2]) > maxPerimeter {
returnArray = nil
returnArray = append(returnArray, sticks[i], sticks[i+1], sticks[i+2])
maxPerimeter = int64(sticks[i]) + int64(sticks[i+1]) + int64(sticks[i+2])
}
}
}
if maxPerimeter == int64(0) {
return []int32{-1}
} else {
sort.Slice(returnArray, func(i, j int) bool { return returnArray[i] < returnArray[j] })
return returnArray
}
}
func hackerrankBirthday(s []int32, d int32, m int32) int32 {
segmentCounter := 0
for i := 0; i < len(s)-int(m)+1; i++ {
sumSegment := s[i]
for offset := 1; offset < int(m); offset++ {
sumSegment += s[i+offset]
}
if sumSegment == d {
segmentCounter++
}
}
return int32(segmentCounter)
}
func migratoryBirds(arr []int32) int32 {
var m = make(map[int32]int32)
for _, value := range arr {
m[value]++
}
highestCount := int32(0)
var highestKeys sortableInt32array
for key, value := range m {
if value > highestCount {
highestKeys = nil
highestKeys = append(highestKeys, key)
highestCount = value
} else if value == highestCount {
highestKeys = append(highestKeys, key)
}
}
sort.Sort(highestKeys)
return highestKeys[0]
}
func setRowToZero(matrix [][]int32, index int) [][]int32 {
for j := range matrix[index] {
matrix[index][j] = 0
}
return matrix
}
func setColumnToZero(matrix [][]int32, j int) [][]int32 {
for i := range matrix {
matrix[i][j] = 0
}
return matrix
}
func isRotatedString(input1, input2 string) bool {
return strings.Contains(input1+input1, input2)
}
func deleteDuplicatesFromList(l *list.List) *list.List {
bitMap := make(map[any]bool)
returnList := list.New()
for e := l.Front(); e != nil; e = e.Next() {
if !bitMap[e.Value] {
returnList.PushFront(e.Value)
bitMap[e.Value] = true
}
}
return returnList
}
func setMatrixRowsColumnsZero(matrix [][]int32) [][]int32 {
rowZero := make([]bool, len(matrix))
columnZero := make([]bool, len(matrix[0]))
for i, row := range matrix {
for j, value := range row {
if value == 0 {
rowZero[i] = true
columnZero[j] = true
}
}
}
for i := range matrix {
if rowZero[i] {
matrix = setRowToZero(matrix, i)
}
}
for j := range matrix[0] {
if columnZero[j] {
matrix = setColumnToZero(matrix, j)
}
}
return matrix
}
func reverseRow(matrix [][]int32, columnIndex int) [][]int32 {
var returnMatrix = make([][]int32, len(matrix))
for i := range returnMatrix {
returnMatrix[i] = make([]int32, len(matrix))
}
for i, row := range matrix {
for j, value := range row {
if columnIndex == j {
returnMatrix[i][j] = matrix[len(matrix)-1-i][j]
} else {
returnMatrix[i][j] = value
}
}
}
return returnMatrix
}
func diagonalDifference(arr [][]int32) int32 {
var forwardSum int32 = 0
var backwardSum int32 = 0
for i, line := range arr {
for j, value := range line {
if i == j {
forwardSum += value
}
if i+j == len(arr)-1 {
backwardSum += value
}
}
}
return int32(math.Abs(float64(forwardSum - backwardSum)))
}
func lonelyInteger(a []int32) int32 {
m := make(map[int32]int32)
for _, value := range a {
m[value]++
}
for key, value := range m {
if value == 1 {
return key
}
}
return 0
}
func gradingStudents(grades []int32) []int32 {
var result []int32
for _, grade := range grades {
if grade >= 38 {
if grade%5 >= 3 {
grade = grade + (5 - (grade % 5))
}
}
result = append(result, grade)
}
return result
}
func matchingStrings(strings []string, queries []string) []int32 {
m := make(map[string]int)
var result []int32
for _, value := range strings {
m[value]++
}
for _, query := range queries {
result = append(result, int32(m[query]))
}
return result
}
func divisibleSumPairs(n int32, k int32, ar []int32) int32 {
counter := 0
for i := 0; i < int(n); i++ {
for j := i + 1; j < int(n); j++ {
if (ar[i]+ar[j])%k == 0 {
counter++
}
}
}
return int32(counter)
}
func isOneEditDifference(input1, input2 string) bool {
if input1 == input2 {
return true
}
runeArr2 := []rune(input2)
var runeArr1 []rune
for _, value := range input1 {
runeArr1 = append(runeArr1, value)
}
// Check if removal of one character possible
if checkInsert(runeArr1, runeArr2) {
return true
}
// Check if insert of one character possible
if checkInsert(runeArr2, runeArr1) {
return true
}
// Check if replace of one character possible
for i := 0; i < len(runeArr2); i++ {
runeArrCopy1 := make([]rune, len(runeArr1))
copy(runeArrCopy1, runeArr1)
runeArrCopy2 := make([]rune, len(runeArr2))
copy(runeArrCopy2, runeArr2)
stringWithOneElementRemoved1 := string(delChar(runeArrCopy1, i))
stringWithOneElementRemoved2 := string(delChar(runeArrCopy2, i))
if stringWithOneElementRemoved1 == stringWithOneElementRemoved2 {
return true
}
}
return false
}
func checkInsert(runeArr1 []rune, runeArr2 []rune) bool {
for i := 0; i < len(runeArr1); i++ {
runeArrCopy := make([]rune, len(runeArr1))
copy(runeArrCopy, runeArr1)
stringWithOneElementRemoved := string(delChar(runeArrCopy, i))
if stringWithOneElementRemoved == string(runeArr2) {
return true
}
}
return false
}
func isPermutationOfPalindrome(input string) bool {
input = strings.ToUpper(input)
input = strings.ReplaceAll(input, " ", "")
var charSet int
for _, value := range input {
normalizedValue := value - 64
if hasBit(charSet, uint(normalizedValue)) {
charSet = clearBit(charSet, uint(normalizedValue))
} else {
charSet = setBit(charSet, uint(normalizedValue))
}
}
mask := charSet - 1
if charSet&mask == 0 {
return true
} else {
return false
}
}
func isPalindrome(input string) bool {
input = strings.ToUpper(input)
input = strings.ReplaceAll(input, " ", "")
if len(input)%2 != 0 {
input = string(delChar([]rune(input), len(input)/2))
}
if input[:len(input)/2] == reverseString(input[len(input)/2:]) {
return true
} else {
return false
}
}
func hasUniqueCharacters(input string) bool {
var charSet [128]bool
for _, char := range input {
if charSet[char] == true {
return false
} else {
charSet[char] = true
}
}
return true
}
func isPermutation(inputString1, inputString2 string) bool {
inputString1 = strings.ToUpper(inputString1)
inputString2 = strings.ToUpper(inputString2)
charMap1 := getCharMapFromString(inputString1)
charMap2 := getCharMapFromString(inputString2)
if reflect.DeepEqual(charMap1, charMap2) {
return true
} else {
return false
}
}
func getCharMapFromString(inputString1 string) map[rune]int {
m := make(map[rune]int)
for _, char := range inputString1 {
m[char] += 1
}
return m
}
func replaceSpacesInCharArray(input []rune) []rune {
var output []rune
for _, value := range input {
if value == ' ' {
output = append(output, '%')
output = append(output, '2')
output = append(output, '0')
} else {
output = append(output, value)
}
}
return output
}
func compressString(input string) string {
var compressedArr []rune
lastChar := ' '
var charCounter int64 = 0
for _, value := range input {
if lastChar != ' ' {
if value != lastChar {
compressedArr = addCompressedValue(compressedArr, lastChar, charCounter)
lastChar = value
charCounter = 1
} else {
charCounter++
}
} else {
lastChar = value
charCounter++
}
}
compressedArr = addCompressedValue(compressedArr, lastChar, charCounter)
return string(compressedArr)
}
func addCompressedValue(compressedArr []rune, lastChar int32, charCounter int64) []rune {
compressedArr = append(compressedArr, lastChar)
for _, value := range strconv.FormatInt(charCounter, 10) {
compressedArr = append(compressedArr, value)
}
return compressedArr
}
func reverseMatrix(matrix [][]int) [][]int {
for i, j := 0, len(matrix)-1; i < j; i, j = i+1, j-1 {
matrix[i], matrix[j] = matrix[j], matrix[i]
}
return matrix
}
func transposeMatrix(matrix [][]int) [][]int {
for i := 0; i < len(matrix); i++ {
for j := 0; j < i; j++ {
matrix[i][j], matrix[j][i] = matrix[j][i], matrix[i][j]
}
}
return matrix
}
func rotateMatrix(matrix [][]int) [][]int {
return transposeMatrix(reverseMatrix(matrix))
}
func formatCamelCase(input []string) []string {
var result []string
for _, value := range input {
var formattedWord string
stringWithoutCommand := value[4:]
if value[0] == 'S' {
var finalArr []rune
for i := 0; i < len(stringWithoutCommand); i++ {
if unicode.IsUpper(rune(stringWithoutCommand[i])) {
if i != 0 {
finalArr = append(finalArr, ' ')
}
finalArr = append(finalArr, unicode.ToLower(rune(stringWithoutCommand[i])))
} else {
finalArr = append(finalArr, rune(stringWithoutCommand[i]))
}
}
formattedWord = string(finalArr)
formattedWord = strings.ReplaceAll(formattedWord, "(", "")
formattedWord = strings.ReplaceAll(formattedWord, ")", "")
result = append(result, formattedWord)
} else {
var finalArr []rune
var spaceOccurred bool
for _, char := range stringWithoutCommand {
if char == ' ' {
spaceOccurred = true
} else {
if spaceOccurred {
finalArr = append(finalArr, unicode.ToUpper(char))
spaceOccurred = false
} else {
finalArr = append(finalArr, char)
}
}
}
if value[2] == 'M' {
finalArr = append(finalArr, '(')
finalArr = append(finalArr, ')')
} else if value[2] == 'C' {
finalArr[0] = unicode.ToUpper(finalArr[0])
}
formattedWord = string(finalArr)
result = append(result, formattedWord)
}
}
return result
}