I need to find a list of strings with input string length n for whom are palindrome by deleting a character from it . The strings are stored inside function DictionaryLookup[]
. Therefore I wrote a support function below
aStringisPal[s_String] :=
DeleteDuplicates[
If[StringLength[#] > 0, s, ## &[]] & /@
Select[If[StringLength[#] == StringLength[s] - 1, #, ## &[]] & /@
Map[StringJoin, Subsets[Characters[s]]], PalindromeQ]]
Then I wrote,
aPalInDictionary[n_Integer?Positive] :=
DeleteCases[
Map[StringJoin,
aStringisPal[#] & /@
Select[DictionaryLookup[], StringLength[#] == n &]], ""]
The output should be a list of words found in DictionaryLookup[] with no duplicates and empty words, which are Palindrome by deleting a character from it .
For example :
aPalInDictionary[3]
should give a list below :
{aah, add, all, Ann, ass, baa, BBC, bee, boo, brr, CNN, coo, DDT, \
Dee, ebb, eek, eel, eff, egg, ell, err, fee, gee, goo, ill, inn, lee, \
Lee, LLB, loo, moo, nee, odd, off, ooh, Orr, pee, ppm, see, shh, ssh, \
tee, too, wee, woo, XXL, zoo}
aPalInDictionary[4]
should give a list below :
{afar, agar, agog, ahas, ajar, alas, alga, anal, anon, aqua, area,
aria, asap, asks, asps, aura, away, ayah, babe, baby, barb, Bede,
bibs, blab, blob, bobs, bomb, bozo, bubo, bubs, bulb, Cara, ceca,
cede, chic, choc, coca, cock, coco, { ...}, Zeke, Zulu}
The code can run with no error. Unfortunately the code above ran too slow, how can I improve the code to make it more efficient. Besides, is there a way to combine these two functions?
I would appreciate if anyone can help.