Write a recursive method named listTwiddles
that accepts a string str
and a Set
of strings representing an English language dictionary and uses exhaustive search and backtracking to print out all those English words that are str
's twiddles.
Two English words are considered twiddles if the letters at each position are either the same, neighboring letters, or next-to-neighboring letters.
For instance, "sparks"
and "snarls"
are twiddles.
Their second and second-to-last characters are different, but 'p'
is two past 'n'
in the alphabet, and 'k'
comes just before 'l'
.
A more dramatic example: "craggy"
and "eschew"
are also twiddles.
They have no letters in common, but craggy's 'c'
, 'r'
, 'a'
, 'g'
, 'g'
, and 'y'
are -2, -1, -2, -1, 2, and 2 away from the 'e'
, 's'
, 'c'
, 'h'
, 'e'
, and 'w'
in "eschew"
.
And just to be clear, 'a'
and 'z'
are not next to each other in the alphabet; there's no wrapping around at all.
(Note: any word is considered to be a twiddle of itself, so it's okay to print str
itself.)
Constraints:
Do not declare any global variables.
You can use any data structures you like, and your code can contain loops, but the overall algorithm must be recursive and must use backtracking.
You are allowed to define other "helper" methods if you like; they are subject to these same constraints.
Do not modify the state of the Lexicon passed in.