From 2ffe6a50e18590f768b5d9aeecf1a036a5645ad1 Mon Sep 17 00:00:00 2001 From: ductnn Date: Sun, 17 Dec 2023 00:23:34 +0700 Subject: [PATCH] add sol --- leetcode/242.ValidAnagram/validAnagram.go | 28 +++++++++++++++++++++++ 1 file changed, 28 insertions(+) create mode 100644 leetcode/242.ValidAnagram/validAnagram.go diff --git a/leetcode/242.ValidAnagram/validAnagram.go b/leetcode/242.ValidAnagram/validAnagram.go new file mode 100644 index 0000000..0f411e1 --- /dev/null +++ b/leetcode/242.ValidAnagram/validAnagram.go @@ -0,0 +1,28 @@ +package main + +import ( + "fmt" +) + +func isAnagram(s string, t string) bool { + if len(s) != len(t) { + return false + } + cnt := [26]int{} + for i := 0; i < len(s); i++ { + cnt[s[i]-'a']++ + cnt[t[i]-'a']-- + } + for _, v := range cnt { + if v != 0 { + return false + } + } + return true +} + +func main() { + s := "anagram" + t := "nagaram" + fmt.Println(isAnagram(s, t)) +}