MCPcopy Create free account
hub / github.com/austingebauer/go-leetcode / isAnagram

Function isAnagram

group_anagrams_49/solution.go:94–131  ·  view source on GitHub ↗
(s1, s2 string)

Source from the content-addressed store, hash-verified

92}
93
94func isAnagram(s1, s2 string) bool {
95 if len(s1) == 0 && len(s2) == 0 {
96 return false
97 }
98
99 // Note: rune is Go terminology for a single Unicode code point.
100 // When looping over a string with range, you get rune (aka int32) values
101 // When looping over a string with for, you get byte (aka uint8) values
102 m := make(map[rune]int)
103 for _, ch := range s1 {
104 _, ok := m[ch]
105 if !ok {
106 m[ch] = 1
107 } else {
108 m[ch]++
109 }
110 }
111
112 for _, ch := range s2 {
113 v, ok := m[ch]
114 if !ok {
115 return false
116 }
117 if v <= 0 {
118 return false
119 }
120
121 m[ch]--
122 }
123
124 for _, v := range m {
125 if v != 0 {
126 return false
127 }
128 }
129
130 return true
131}

Callers 1

groupAnagrams0Function · 0.70

Calls

no outgoing calls

Tested by

no test coverage detected