Я не уверен, что одобряю обман - я болею за твою жену! Но это интересная проблема, поэтому я буду ее игнорировать ;-).
Вот другой подход. Возьмите все слова в dat
и отсортируйте их по алфавиту; также сортируйте буквы в вашем наборе ввода в алфавитном порядке. Мы можем использовать простое регулярное выражение, чтобы найти слова в словаре, которые содержат не более указанного числа токенов каждой буквы.
library(tidyverse)
# Get the dictionary.
dat = read_lines("https://raw.githubusercontent.com/dwyl/english-words/master/words.txt")
# A function that returns possible words given a set of letters. The letters
# are provided as a single string argument (e.g., "ilrfle").
possible.words = function(letters) {
# Filter to words that contain only letters in the list. This step isn't
# strictly necessary, but it gives later steps a smaller list to have to
# process.
right.letters = unique(dat[grepl(paste("^[", letters, "]+$", sep = ""), dat)])
# We're going to create a data frame where the first column is the word and
# the second column is the word with its characters sorted in alphabetical
# order. Start with the first column.
df = data.frame(word = right.letters, stringsAsFactors = F)
# Now add the second column. This could probably be done in dplyr, but my
# initial attempt with mutate didn't work, and for the examples I've tried
# the loop actually doesn't take too long.
for(i in 1:nrow(df)) {
df$sorted.word[i] = paste(sort(unlist(strsplit(df$word[i], ""))), collapse = "")
}
# Now we want to extract words that contain only as many tokens of each
# letter as there were in the initial set. We can use a regular expression
# to compare the (sorted) letters of the initial set to the (sorted) letters
# of each word, where each letter in the initial set is optional.
sorted.letters.regex = paste(sort(paste(unlist(strsplit(letters, "")), "?", sep = "")), collapse = "")
df = df %>%
filter(grepl(paste("^", sorted.letters.regex, "$", sep = ""), sorted.word))
return(df$word)
}