Given two strings, string1
and string2
, determine if string2
is an anagram of string1
. 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: str1 = "listen" , str2 = "silent"
Output: true
function isAnagramSorting(s, t) {
return s.split('').sort().join('') === t.split('').sort().join('');
}
const str11 = "listen";
const str21 = "silent";
isAnagramSorting(str11,str21); //output: true
function isAnagramFrequencyObject(s, t) {
if (s.length !== t.length) return false;
const count = {};
for (const char of s) count[char] = (count[char] || 0) + 1;
for (const char of t) {
if (!count[char]) return false;
count[char]--;
if (count[char] < 0) return false;
}
return true;
}
const str11 = "listen";
const str21 = "silent";
isAnagramFrequencyObject(str11,str21); //output: true
function isAnagramFrequencyArray(s, t) {
if (s.length !== t.length) return false;
const count = new Array(26).fill(0);
const base = 'a'.charCodeAt(0);
for (const char of s) count[char.charCodeAt(0) - base]++;
for (const char of t) {
if (--count[char.charCodeAt(0) - base] < 0) return false;
}
return true;
}
const str11 = "listen";
const str21 = "silent";
isAnagramFrequencyArray(str11,str21); //output: true
function isAnagramMap(s, t) {
if (s.length !== t.length) return false;
const count = new Map();
for (const char of s) count.set(char, (count.get(char) || 0) + 1);
for (const char of t) {
if (!count.has(char)) return false;
count.set(char, count.get(char) - 1);
if (count.get(char) < 0) return false;
}
return true;
}
const str11 = "listen";
const str21 = "silent";
isAnagramMap(str11,str21); //output: true
function isAnagramSet(s, t) {
if (s.length !== t.length) return false;
const sSet = new Set(s);
const tSet = new Set(t);
if (sSet.size !== tSet.size) return false;
for (const char of sSet) {
if (!tSet.has(char)) return false;
}
return true;
}
const str11 = "listen";
const str21 = "silent";
isAnagramSet(str11,str21); //output: true
function isAnagramReduce(s, t) {
if (s.length !== t.length) return false;
const count = s.split('').reduce((acc, char) => {
acc[char] = (acc[char] || 0) + 1;
return acc;
}, {});
for (const char of t) {
if (!count[char]) return false;
count[char]--;
if (count[char] < 0) return false;
}
return true;
}
const str11 = "listen";
const str21 = "silent";
isAnagramReduce(str11,str21); //output: true
function isAnagramCharCode(s, t) {
if (s.length !== t.length) return false;
const count = new Array(26).fill(0);
const base = 'a'.charCodeAt(0);
for (const char of s) count[char.charCodeAt(0) - base]++;
for (const char of t) {
if (--count[char.charCodeAt(0) - base] < 0) return false;
}
return true;
}
const str11 = "listen";
const str21 = "silent";
isAnagramCharCode(str11,str21); //output: true
function isAnagramSome(s, t) {
if (s.length !== t.length) return false;
const sCount = Array.from(s).reduce((acc, char) => {
acc[char] = (acc[char] || 0) + 1;
return acc;
}, {});
return !Array.from(t).some(char => {
if (!sCount[char]) return true;
sCount[char]--;
return sCount[char] < 0;
});
}
const str11 = "listen";
const str21 = "silent";
isAnagramSome(str11,str21); //output: true
function isAnagramCounter(s, t) {
if (s.length !== t.length) return false;
const count = {};
for (const char of s) count[char] = (count[char] || 0) + 1;
for (const char of t) {
if (!count[char]) return false;
count[char]--;
if (count[char] < 0) return false;
}
return true;
}
const str11 = "listen";
const str21 = "silent";
isAnagramCounter(str11,str21); //output: true
function isAnagramSplitJoin(s, t) {
if (s.length !== t.length) return false;
const count = Array(26).fill(0);
const base = 'a'.charCodeAt(0);
s.split('').forEach(char => count[char.charCodeAt(0) - base]++);
t.split('').forEach(char => {
if (--count[char.charCodeAt(0) - base] < 0) return false;
});
return true;
}
const str11 = "listen";
const str21 = "silent";
isAnagramSplitJoin(str11,str21); //output: true