Drill

ProblemsGo › warmup

Are these two an anagram

mediumwarmupHash mapsStringsSortingGo

A word game checks whether one phrase uses exactly the same letters as another.

isAnagram(text: string, other: string) → bool

Go needs a compiler and Drill does not host one yet, so this page is the reference rather than an exercise: the problem, worked examples, and the solution in full. To type it out, the same problem runs in Python.

Solve it in Python →

Where you start

func isAnagram(text string, other string) bool {
	
}

Worked examples

CallResult
isAnagram("Listen", "Silent")true
isAnagram("a b", "ba")true
isAnagram("hello", "world")false
isAnagram("abc", "ab")false

Hint

Strip and lowercase both, then compare the sorted characters, or count them.

Reference solution in Go
func isAnagram(text string, other string) bool {
	clean := func(s string) string {
		b := []byte{}
		for i := 0; i < len(s); i++ {
			c := s[i]
			if c == ' ' || c == '\t' || c == '\n' {
				continue
			}
			if c >= 'A' && c <= 'Z' {
				c += 32
			}
			b = append(b, c)
		}
		sort.Slice(b, func(i, j int) bool { return b[i] < b[j] })
		return string(b)
	}
	return clean(text) == clean(other)
}

The same problem in another language

More warmup problems in Go