The Anagrams Code Challenge

Gage Thornberry
3 min readMay 17, 2020

--

What Is An Anagram?

One string is an anagram of another if it uses the same characters in the same quantity. Only consider characters, not spaces or punctuation. Consider capital letters to be the same as lower case.

// — — Examples

// anagrams(‘rail safety’, ‘fairy tales’) → True

// anagrams(‘RAIL! SAFETY!’, ‘fairy tales’) → True

// anagrams(‘Hi there’, ‘Bye there’) → False

Hey there guys, today I’m going go be going over the Anagrams code challenge, and a couple of different ways that you can go about solving it. So what makes two different strings anagrams of each other?

An anagram is a string that consists of the same letters, and the same amount of those letters. In this example we will be taking in two different strings and returning true or false if they are anagrams of each other. In the examples above, there are a couple things to note about what we are receiving in our string arguments.

1. The string can contain non-alphabet characters, such as !?#, numbers, and spaces. We want to ignore EVERYTHING except for the letters. The other thing to keep in mind is that our input may contain capital letters.

How I Solved It -

Alright, let’s solve this thing. So this post isn’t super well organized but I wanted to just explain my thinking on this and then start coding it for you. Feel free to follow along if you want to try this challenge. I’ll put a link at the top of the page.

Okay, so what are my main thoughts about this? I want to solve this in a way that will be fast, straight to the point, and easy to read. In simple terms, I want to format the string to remove any unwanted characters, sort the string, and compare it to the second string.

NOTE-There are more than just one way to solve this.

This is O(log n) + O(n) (slightly slower but much more readable and to the point)

An Alternate Route -

Another way we could solve this is by removing the unwanted characters from our string, and then creating an object that would keep track of the letters in our string, and how many times that letter occurred. Then we can look through each key in the first object and say “does the second object contain this key? And if so, how many times did it occur?

This is fast because the second that it does a comparison that isn’t true, it can return false and not have to look through the entire object. This is more code, but also slightly faster because of the O(n) linear runtime.

Give It A Try -

Now that we’ve talked about the different solutions to this problem, I want you to give it a try and see what you come up with. Keep in mind that any character can be present within our input strings, and they should probably removed off the bat.

Lets Look At Some Code -

Let’s break this down step by step. The first thing we did was use regex to replace the items in our string in place. This patters translates to is this element part of the alphabet? If so, leave it. If not, let’s replace it with nothing. (aka remove it)

Next we want to get it ready for the comparison. Lowercase it, split it into an array so it can be sorted, and join it back into a string so that we can compare the two. That’s really it.

This is a rather simple challenge but if you’re not warmed up then I believe it could catch you off guard.

Thanks for reading!

--

--