242. 有效的字母异位词

242. 有效的字母异位词 #

题目地址 #

解题思路 #

map #

具体实现 #

package main

import "fmt"

func isAnagram(s string, t string) bool {
	if len(s) != len(t) {
		return false
	}

	var arr [26]int

	for i := 0; i < len(s); i++ {
		arr[s[i]-'a']++
	}
	for i := 0; i < len(t); i++ {
		arr[t[i]-'a']--
	}

	for _, each := range arr {
		if each > 0 {
			return false
		}
	}

	return true
}

func main() {
	fmt.Println(isAnagram("abc", "bcc"))
}
package main

import "fmt"

func isAnagram(s string, t string) bool {
	if len(s) != len(t) {
		return false
	}

	count := make(map[int32]int)
	for _, w := range s {
		if _, ok := count[w]; ok {
			count[w]++
		} else {
			count[w] = 1
		}
	}

	for _, w := range t {
		if _, ok := count[w]; ok {
			count[w]--
		}
	}

	for _, v := range count {
		if v != 0 {
			return false
		}
	}

	return true
}

func main() {
	fmt.Println(isAnagram("anagram", "nagaram"))
}