-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path944.go
73 lines (59 loc) · 1.66 KB
/
944.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
func minDeletionSize(strs []string) int {
Ans := 0
numberOfCol := len(strs[0])
numberOfRow := len(strs)
for j := 0; j < numberOfCol; j++ {
var tmpString string
var originalString string
for i := 0; i < numberOfRow; i++ {
tmpString += string(strs[i][j])
originalString += string(strs[i][j])
}
//sort.Strings(tmpString) --> gives error
sortedString := sortString(tmpString)
//fmt.Println(tmpString)
if sortedString != originalString {
Ans++
}
}
return Ans
}
func sortString(input string) string{
runeArray := []rune(input) // convert in Rune [act like an array]
sort.Sort(sortRuneString(runeArray))
fmt.Println(string(runeArray))
return string(runeArray)
}
type sortRuneString []rune
func (s sortRuneString) Swap(i, j int) {
s[i], s[j] = s[j], s[i] // swap the values of s[i] and s[j]
}
func (s sortRuneString) Less(i, j int) bool {
return s[i] < s[j] // sorting in ascending
}
func (s sortRuneString) Len() int {
return len(s) // return the length
}
/*
Focus:
Sort a string. First make a "rune" array. Then send them in sort.Sort() properties.
*/
/*
Signature of sort.Sort() function: func Sort(data Interface)
Definition of Interface :
type Interface interface {
// Len is the number of elements in the collection.
Len() int
// Less reports whether the element with index i
// must sort before the element with index j.
Less(i, j int) bool
// Swap swaps the elements with indexes i and j.
Swap(i, j int)
}
So whatever we want to sort using the sort.Sort function then that needs to implement above three functions
1. Swap(i, j) int
2. Less(i, j int) bool
3. Len()
*/
/*
*/