Skip to content

Instantly share code, notes, and snippets.

@joshua-feldman
Created April 24, 2021 16:36
Show Gist options
  • Save joshua-feldman/a42a63b556d34da2d8dc65081f552152 to your computer and use it in GitHub Desktop.
Save joshua-feldman/a42a63b556d34da2d8dc65081f552152 to your computer and use it in GitHub Desktop.
Function to check whether two words are anagrams
# FUNCTION TO CHECK WHETHER TWO WORDS ARE ANAGRAMS
# Based on the following logic by @fermatslibrary:
# 1. Map each of the 26 English characters to a prime number
# 2. Multiply the characters of each word
# 3. Two words are anagrams if their products are the same
# This works because every integer is a prime or a unique product of primes
library(magrittr)
is_anagram <- function(word1, word2) {
letters_numbers <- data.frame(letter = letters, number = primes::generate_n_primes(26))
word1_split <- word1 %>%
tolower() %>%
stringr::str_remove_all(" ") %>%
tm::removePunctuation() %>%
strsplit(split = "") %>%
unlist() %>%
data.frame() %>%
dplyr::left_join(letters_numbers, by = c(`.` = "letter"))
word2_split <- word2 %>%
tolower() %>%
stringr::str_remove_all(" ") %>%
tm::removePunctuation() %>%
strsplit(split = "") %>%
unlist() %>%
data.frame() %>%
dplyr::left_join(letters_numbers, by = c(`.` = "letter"))
word1_prod <- prod(word1_split$number)
word2_prod <- prod(word2_split$number)
return(word1_prod == word2_prod)
}
is_anagram("I am Lord Voldemort", "Tom Marvolo Riddle")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment