-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathone-away.go
59 lines (45 loc) · 927 Bytes
/
one-away.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
package main
import "fmt"
import "strings"
var str1 string
var str2 string
var strarr1 []string
var strarr2 []string
var m map[string]int
var count int
func main() {
str1 = "bales"
str2 = "bill"
//create a hash table for string 1
strarr1 = strings.Split(str1, "")
strarr2 = strings.Split(str2, "")
m = make(map[string]int)
m[strarr1[0]] = 1
for i := 1; i < len(strarr1); i++ {
v, ok := m[strarr1[i]]
if ok == false {
m[strarr1[i]] = 1
continue
}
m[strarr1[i]] = (v + 1)
}
fmt.Println(m)
//compare each character of string 2 with hash table and count differences
count = 0
for i := 0; i < len(strarr2); i++ {
v, ok := m[strarr2[i]]
if ok == false || v == 0 {
count = count + 1
continue
}
m[strarr1[i]] = (v - 1)
}
fmt.Println(m)
//if difference in char count > 1 then false else true
if count > 1 {
fmt.Println("false")
}
if count <= 1 {
fmt.Println("true")
}
}