-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
71 lines (58 loc) · 1.82 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
package main
import "fmt"
// Time Complexity: O(2n) ~ O(n) where m is number of characters in both strings, and we loop over all of them
// Space Complexity: O(26) = O(1), as we use only space required for the extra map with chars and counts
func isAnagram(s string, t string) bool {
// if they are different in length then for sure not anagram
if len(s) != len(t) {
return false
}
// first try building the map of characters and how many times they used in the `s`
charCounts := make(map[rune]int)
for _, ch := range s {
charCounts[ch]++ // charCounts[ch] gives you back 0 if value not present (default value of missing item)
}
// now `t` need's to use all the chars from `s`
for _, ch := range t {
// if value is not present this is for sure is not anagram
if _, exists := charCounts[ch]; !exists {
return false
}
// mark as used
charCounts[ch]--
// and delete from the map
if charCounts[ch] <= 0 {
delete(charCounts, ch)
}
}
// when nothing left then probably anagram
return len(charCounts) == 0
}
// Time Complexity: O(n) loop once
// Space Complexity: O(1), as we use only space required for the extra array with chars and counts
func isAnagramFaster(s string, t string) bool {
if len(s) != len(t) {
return false
}
// here we gonna store how many times we've seen a letter
// 26 is number of lowercase English letters, more we do nto need
var counter [26]int
for i := range s {
// calculate index of character in the counter array
chrS := s[i] - 'a'
chrT := t[i] - 'a'
// increase for S and decrease for T
counter[chrS]++
counter[chrT]--
}
// loop over the small array of chars and see if it's all used
for _, cnt := range counter {
if cnt != 0 {
return false
}
}
return true
}
func main() {
fmt.Printf("anagram and nagaram = %t", isAnagramFaster("anagram", "nagaram"))
}