diff --git a/valid-anagram/SamTheKorean.go b/valid-anagram/SamTheKorean.go new file mode 100644 index 000000000..26386c1ef --- /dev/null +++ b/valid-anagram/SamTheKorean.go @@ -0,0 +1,22 @@ +// TC : O(n) : it iterates for the length of nums +// SC : O(n) : we make hashmap as the size of the given string +func isAnagram(s string, t string) bool { + if len(s) != len(t) { + return false + } + + letterCounts := make(map[rune]int) + + for _, ch := range s { + letterCounts[ch]++ + } + + for _, ch := range t { + letterCounts[ch]-- + if letterCounts[ch] < 0 { + return false + } + } + + return true +}