Created
April 24, 2021 16:36
-
-
Save joshua-feldman/a42a63b556d34da2d8dc65081f552152 to your computer and use it in GitHub Desktop.
Function to check whether two words are anagrams
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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