add new solutions
This commit is contained in:
parent
ca24d0a56a
commit
0c73608ce5
36 changed files with 791 additions and 0 deletions
37
solutions/22/q2273/solution.go
Normal file
37
solutions/22/q2273/solution.go
Normal file
|
|
@ -0,0 +1,37 @@
|
|||
package q2273
|
||||
|
||||
var buf = [26]int{}
|
||||
var bufZ = [26]int{}
|
||||
|
||||
func isAnagram(a, b string) bool {
|
||||
if len(a) != len(b) {
|
||||
return false
|
||||
}
|
||||
seen := buf[:]
|
||||
copy(seen, bufZ[:])
|
||||
for i := range len(a) {
|
||||
c := a[i] - 'a'
|
||||
seen[c]++
|
||||
}
|
||||
for i := range len(b) {
|
||||
c := b[i] - 'a'
|
||||
seen[c]--
|
||||
if seen[c] < 0 {
|
||||
return false
|
||||
}
|
||||
}
|
||||
return true
|
||||
}
|
||||
|
||||
func removeAnagrams(words []string) []string {
|
||||
p := 0
|
||||
for i := 1; i < len(words); i++ {
|
||||
if !isAnagram(words[p], words[i]) {
|
||||
p++
|
||||
words[p] = words[i]
|
||||
}
|
||||
}
|
||||
return words[:p+1]
|
||||
}
|
||||
|
||||
var _ = removeAnagrams
|
||||
Loading…
Add table
Add a link
Reference in a new issue