Check if two strings are Anagram.
Can anyone help me further i have found the occurances of each character.
19 Replies
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"
"gram" is not anagram of the word "anagram"
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
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 .
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
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:
What about counting letter occurrences of both words and comparing the results, reed/deer r-1;e-2;d-1?
did you go through this ?
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
Oh yeah I haven’t checked just read your answers, imo it’s the more efficient way, although involves a bit more code
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
Yeah yeah I saw OPs answer 😁
oki xD
Thanks @glutonium @Chris @Hashi
wlcm
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 😁
Thinking
I guess the check is there, if not could you please figure or point it where