aboutsummaryrefslogtreecommitdiffstats
path: root/1657/main.go
blob: 7dfdcb40ffabcc2f4522142f593911ec7ea27f68 (plain) (blame)
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
package main

import "fmt"

func dumpDict(dict map[int]int) {
	for key, val := range dict {
		fmt.Println(key, val)
	}
}

func getWordDict(word string) map[int]int {
	dict := make(map[int]int)

	for i := 0; i < len(word); i++ {
		if _, ok := dict[int(word[i])]; ok {
			dict[int(word[i])]++
		} else {
			dict[int(word[i])] = 1
		}
	}

	return dict
}

func closeStrings(word1 string, word2 string) bool {

	if len(word1) != len(word2) {
		return false
	}

	dict1 := getWordDict(word1)
	dict2 := getWordDict(word2)

	// dumpDict(dict1)
	// fmt.Println()
	// dumpDict(dict2)

	if len(dict1) != len(dict2) {
		return false
	}

	for key, _ := range dict1 {
		if _, ok := dict2[key]; ok {
			continue
		} else {
			return false
		}
	}

	delete_key := 0
	matched := false
	for _, val := range dict1 {
		for key2, val2 := range dict2 {
			if val == val2 {
				delete(dict2, key2)
				matched = true
				delete_key = key2
				break
			}
		}

		if matched == true {
			matched = false
			delete(dict2, delete_key)
		} else {
			return false
		}
	}

	return true
}

func main() {
	word1 := "abbzzca"
	word2 := "babzzcz"
	fmt.Println(closeStrings(word1, word2))
}