Check if two strings are Anagram.

Can anyone help me further i have found the occurances of each character.

function areAnagrams(str1, str2) {
const result1 = {};
const result2 = {};
for (let i = 0; i <= str1.length - 1; i++) {

if (!result1[str1[i]]) {
result1[str1[i]] = 1;
}
else {
result1[str1[i]] += 1
}
}
console.log(result1);
for (let i = 0; i <= str2.length - 1; i++) {
if (!result2[str2[i]]) {
result2[str2[i]] = 1
}
else {
result2[str2[i]] += 1;

}
}
console.log(result2);



}
console.log(areAnagrams("listen", "silent"))

function areAnagrams(str1, str2) {
const result1 = {};
const result2 = {};
for (let i = 0; i <= str1.length - 1; i++) {

if (!result1[str1[i]]) {
result1[str1[i]] = 1;
}
else {
result1[str1[i]] += 1
}
}
console.log(result1);
for (let i = 0; i <= str2.length - 1; i++) {
if (!result2[str2[i]]) {
result2[str2[i]] = 1
}
else {
result2[str2[i]] += 1;

}
}
console.log(result2);



}
console.log(areAnagrams("listen", "silent"))
19 Replies
glutonium
glutonium4mo ago
if u wanna check if string1 is anagram of string2, then simple just check if all the letters in string1 exists in string2 or not for example u wanna see if "gram" is anagram of the word "anagram" (which it is), you'd check if letters in the word "gram" exists in the word "anagram"
Vandana
VandanaOP4mo ago
"gram" is not anagram of the word "anagram"
glutonium
glutonium4mo ago
isnt anagram where u can make a word out of the letters of a diff word? i searched it up.. it makes sense now, i actually had the concept of anagram wrong
Vandana
VandanaOP4mo ago
So i tried to collect the occurances of each character and store it in two objects.Now i need to compare them but objects are references .
glutonium
glutonium4mo ago
so the problem u have rn is how u can compare two objects? well, comparing two objects, ig u have to compare their keys and their values u can use Object.keys() and Object.values() to get array of their keys and values u can try getting the array of key and value for each object then match key to key and value to value for all 4 arryas there could be a better ways to match two objects but i cant think of any other rn nonetheless, the apporach u r taking will work but u can try better ways to do it, this is extra complicated i tried to solve it, and i did manage to find a more simpler way to do it it is low compared to other submissions but it works i can share if u want, but only once u have solved it on your own
glutonium
glutonium4mo ago
/**
* @param {string} s
* @param {string} t
* @return {boolean}
*/
var isAnagram = function(s, t) {
if(t.length !== s.length) return false;
if(t.length === 0) return true;

for(let i=0; i<t.length; i++) {
if(s.includes(t[i])) {
s = s.replace(t[i], '');
continue;
}else {
return false;
}
}

return true;
};
/**
* @param {string} s
* @param {string} t
* @return {boolean}
*/
var isAnagram = function(s, t) {
if(t.length !== s.length) return false;
if(t.length === 0) return true;

for(let i=0; i<t.length; i++) {
if(s.includes(t[i])) {
s = s.replace(t[i], '');
continue;
}else {
return false;
}
}

return true;
};
Chris Bolson
Chris Bolson4mo ago
As with all of these sort of challenges, there are several ways to achieve the result, some more optimal than others. I am the first to admit that I am terrible at them! As glutonium has just posted whilst I was writing this, the first thing you should try to do is return the "imposible" cases as false (or true, depending on the actual challenge) as soon as possible to avoid running unnecessary code and checks. In this case, if the length of each of the strings is not the same, then it will be false, regardless of the actual content so you can return "false" straight away. You then need to do your comparison code, returning "false" as soon as you find a non-match. This of course is the tricky part. Finally, if your strings make it through the tests, you return "true" as, presumably, that is the only option left. That said, in this particular case I would probably convert the 2 strings to arrays, sort them, join them together again with the new sorted order and then compare the new sorted strings but, as I say, there are several ways to do this comparison. Just to show how I might have done this using the sort() method:
function areAnagrams(str1, str2) {
if (str1.length !== str2.length) return false;
const str1Sorted = str1.split('').sort().join('');
const str2Sorted = str2.split('').sort().join('');
return str1Sorted === str2Sorted;
}
function areAnagrams(str1, str2) {
if (str1.length !== str2.length) return false;
const str1Sorted = str1.split('').sort().join('');
const str2Sorted = str2.split('').sort().join('');
return str1Sorted === str2Sorted;
}
Hashi
Hashi4mo ago
What about counting letter occurrences of both words and comparing the results, reed/deer r-1;e-2;d-1?
Vandana
VandanaOP4mo ago
did you go through this ?
glutonium
glutonium4mo ago
that's what OP did that's pretty clever tho, i have to give u that i at first tried with charCodeAt() lol like use that to hash both strings by summing up all the char code for each word then matching both sum but ya that doesnt really work
Hashi
Hashi4mo ago
Oh yeah I haven’t checked just read your answers, imo it’s the more efficient way, although involves a bit more code
glutonium
glutonium4mo ago
i did not take that approach xD the original poster did, the codepen u can check there. the OP did what u suggested that also works fine ya
Hashi
Hashi4mo ago
Yeah yeah I saw OPs answer 😁
glutonium
glutonium4mo ago
oki xD
Vandana
VandanaOP4mo ago
Thanks @glutonium @Chris @Hashi
glutonium
glutonium4mo ago
wlcm
Hashi
Hashi4mo ago
You could probably add check in your second word iteration, if a letter doesn’t exist in first word result {} so you return false earlier, although it’s definitely overkill 😁
Vandana
VandanaOP4mo ago
Thinking I guess the check is there, if not could you please figure or point it where
Want results from more Discord servers?
Add your server