Ad space (banner)
๐ŸŸฃJulia Lessons
Lesson 64 / 68

Testing whether two words are anagrams

This lesson covers testing for anagrams in Julia, with the aim of understanding how to sort and compare strings.

An anagram is a word made by rearranging the letters of another (listen and silent, for instance). Sort the letters of both and compare.

The sample code lower-cases with sort(collect(lowercase(a))), converts to an array of characters, and sorts, doing the same to the other string before comparing. "listen" and "silent" have the same letters, so sorted they match.

A common early stumble is not allowing for case. The sample unifies case with lowercase() before comparing; forget that and the same word in different cases is judged different.

It is a basic technique for comparing strings while ignoring order. For longer strings, counting how often each character appears is faster.

๐Ÿ“– Reference code
โœ๏ธ Your code
Type your code, then press "Run"

๐Ÿงช This site can't compile or run Julia directly, so it checks on the spot whether what you typed matches the reference code (scoring happens entirely in your browser โ€” nothing is sent anywhere).

Ad space (banner)
Ad space (in-article)