From 6f67b79b8ce67dbd2ddbfe845e2e2d120727bbb5 Mon Sep 17 00:00:00 2001 From: terminaldweller Date: Thu, 28 Jul 2022 17:37:09 +0430 Subject: update --- 242/go.mod | 3 +++ 242/main.go | 53 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 56 insertions(+) create mode 100644 242/go.mod create mode 100644 242/main.go (limited to '242') diff --git a/242/go.mod b/242/go.mod new file mode 100644 index 0000000..99ad2d5 --- /dev/null +++ b/242/go.mod @@ -0,0 +1,3 @@ +module 242 + +go 1.17 diff --git a/242/main.go b/242/main.go new file mode 100644 index 0000000..49b6faf --- /dev/null +++ b/242/main.go @@ -0,0 +1,53 @@ +package main + +func isAnagram_v_1(s string, t string) bool { + s_map := make(map[rune]int, len(s)) + t_map := make(map[rune]int, len(t)) + for _, character := range s { + s_map[character]++ + } + for _, character := range t { + t_map[character]++ + } + + if len(s_map) != len(t_map) { + return false + } + for k, v := range s_map { + value, ok := t_map[k] + if !ok { + return false + } + if value != v { + return false + } + } + + return true +} + +func isAnagram(s string, t string) bool { + s_map := make([]int, 26) + t_map := make([]int, 26) + for _, character := range s { + s_map[int(character-'a')]++ + } + for _, character := range t { + t_map[int(character-'a')]++ + } + + if len(s_map) != len(t_map) { + return false + } + for i := 0; i < 26; i++ { + if s_map[i] != t_map[i] { + return false + } + } + + return true +} + +func main() { + isAnagram("anagram", "nagaram") +} -- cgit v1.2.3