Ad space (banner)
๐ŸนGo Lessons
Lesson 64 / 68

Checking Whether Two Words Are Anagrams

In this lesson you will learn how to check whether two words are anagrams in Go, covering how to sort and compare strings. This is for anyone searching for "Go anagram check implementation".

An anagram is a different word made from the same letters rearranged (for example, listen and silent). Sort the letters of both strings and see whether they match.

The sample code lower-cases each string and splits it into a slice of single characters with strings.Split(strings.ToLower(a), ""), sorts with sort.Strings(), and joins back with strings.Join() before comparing.

A common stumbling block for beginners is not allowing for case differences. The sample folds to lower case with ToLower() before comparing; skip that and the same word in different cases is judged to be different.

For long strings, counting how often each character occurs is faster, and the same idea extends to building simple code-breaking games.

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

๐Ÿงช This site can't compile or run Go 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)