Problem - Leetcode
Given two strings s
and t
, return true
if t
is an anagram of s
, and false
otherwise.
An Anagram is a word or phrase formed by rearranging the letters of a different word or phrase, typically using all the original letters exactly once.
Input: s = "anagram", t = "nagaram"
Output: true
Input: s = "rat", t = "car"
Output: false
Constraints:
1 <= s.length, t.length <= 5 * 10<sup>4</sup>
s
andt
consist of lowercase English letters.
Answer-1 in Golang
func isAnagram(s string, t string) bool {
if len(s) != len(t) {
return false
}
mapS, mapT := make(map[uint8]int), make(map[uint8]int)
for i := 0; i < len(s); i++ {
mapS[s[i]]++
mapT[t[i]]++
}
for key, Value := range mapS {
if mapT[key] != Value {
return false
}
}
return true
}
This code defines a function called isAnagram
that takes two string inputs, s
and t
, and returns a boolean value indicating whether they are anagrams of each other.
An anagram is a word or phrase formed by rearranging the letters of another word or phrase, typically using all the original letters exactly once. In this case, the code is checking if two strings, s
and t
, are anagrams of each other.
Let’s break down the code step by step:
The function
isAnagram
takes two string parameters,s
andt
, and returns a boolean value indicating whether they are anagrams of each other.The first check compares the lengths of strings
s
andt
. If they are not of the same length, it means they cannot be anagrams. In this case, the function immediately returnsfalse
.Two maps,
mapS
andmapT
, are initialized using the built-inmake
function. These maps will be used to store the frequency of characters in stringss
andt
respectively. The keys of these maps are of typeuint8
, representing byte values (essentially ASCII values), and the values are integers representing the frequency of each character.A loop runs through each character of strings
s
andt
simultaneously (since they have the same length). Inside the loop, the frequencies of characters in bothmapS
andmapT
are updated. This is achieved by using the characters as keys and incrementing their corresponding values in the maps.After populating both maps with character frequencies, a second loop iterates through the keys in
mapS
. For each key, it checks if the frequency of that character inmapT
is equal to the frequency inmapS
. If not, it means that the character frequencies between the two strings are not the same, which would indicate that the strings are not anagrams. In this case, the function returnsfalse
.If the second loop completes without returning
false
, it means that the character frequencies in both maps are the same, which implies that the input stringss
andt
are anagrams of each other. Thus, the function returnstrue
.
In summary, this code determines whether two input strings are anagrams by comparing their lengths and checking the frequencies of characters in each string. If the lengths are different or if the character frequencies do not match, the function returns false
, indicating that the strings are not anagrams. If the character frequencies are the same, the function returns true
, indicating that the strings are anagrams.
Answer-2 Top Runtime in Golang
func isAnagram(s string, t string) bool {
m := make(map[rune]int, len(s))
for _, r := range s {
m[r]++
}
for _, r := range t {
m[r]--
}
for _, v := range m {
if v != 0 {
return false
}
}
return true
}
This code defines a function called isAnagram
that checks whether two input strings s
and t
are anagrams of each other. Anagrams are words or phrases that have the same characters but in a different order.
Here's a detailed explanation of how the code works:
The function
isAnagram
takes two string arguments,s
andt
.It starts by creating an empty map named
m
using themake
function. This map will store the frequency of each character in thes
string. The map's keys are of typerune
(Unicode characters), and the values are integers representing the frequency of each character.The first loop iterates over each character
r
in thes
string. It increments the value associated with the characterr
in the mapm
by 1. This step counts the frequency of characters in thes
string.The second loop iterates over each character
r
in thet
string. It decrements the value associated with the characterr
in the mapm
by 1. This step simulates subtracting the frequency of characters present in thet
string.After both loops complete, the map
m
will hold the difference in character frequencies between the two strings. Ifs
andt
are anagrams, the mapm
should have all values equal to 0, because the frequencies should cancel each other out.The final loop iterates over the values in the map
m
. If any value is not equal to 0, it means the frequencies of characters ins
andt
did not cancel out, indicating that the strings are not anagrams. In this case, the function returnsfalse
.If all values in the map
m
are equal to 0, then the stringss
andt
are anagrams, and the function returnstrue
.
In summary, the code uses a map to store the frequency of characters in the input strings and then checks if the frequency differences between the strings are all zero to determine if the strings are anagrams.
Answer-3 Top Memory in Golang
func isAnagram(s string, t string) bool {
if len(s) != len(t) {
return false
}
m := make(map[byte]int, 0)
for i := 0; i < len(s); i++ {
m[s[i]]++
m[t[i]]--
}
for _, v := range m {
if v != 0 {
return false
}
}
return true
}
This code defines a function called isAnagram
that takes two string parameters s
and t
and returns a boolean value indicating whether the two input strings are anagrams of each other or not. Anagrams are words or phrases that are formed by rearranging the letters of another word or phrase.
Here's a breakdown of how the code works:
The function begins by checking if the lengths of strings
s
andt
are equal. If they are not equal, it immediately returnsfalse
, since two strings with different lengths cannot be anagrams of each other.If the lengths of the strings are equal, the function proceeds to create a map called
m
to keep track of the frequency of each character in the strings. The map is of typemap[byte]int
, where the keys are individual characters (represented as bytes) and the values are integers representing the frequency of each character.A loop runs through each character index from 0 to the length of the strings minus 1 (i.e., for each character in the strings). Inside the loop:
The frequency count of the character
s[i]
is increased in the mapm
using the++
operator.The frequency count of the character
t[i]
is decreased in the mapm
using the--
operator.
After both strings have been processed in the loop, the code enters another loop using a range-based iteration on the values of the map
m
. For each valuev
in the map:- If
v
is not equal to 0, it means that the frequencies of characters in the two stringss
andt
are not balanced, indicating that they are not anagrams. In this case, the function returnsfalse
.
- If
If all the values in the map
m
are equal to 0, it means that the frequencies of characters in the two strings are balanced, and the function returnstrue
, indicating that the input stringss
andt
are indeed anagrams of each other.
To summarize, this code uses a character frequency map to determine whether two strings are anagrams by comparing the frequency of characters in each string. If the frequencies are the same for all characters, the strings are anagrams, and the function returns true
. Otherwise, it returns false
.