I have a huge list of words and I need to find all the words that can be reduced to a one letter word by removing one letter at a time.
//e.g. Assume all these words are real and in the dictionary
apple
pple //remove a
ppe //remove l
pe //remove p
e //remove p
So I would need to save apple as a reducible word
Other requirements:
I need to output every word that can be reduced AND every word it reduces to of size length-1, so every word that it produces by removing one letter needs to be output.
Keep track of how many words can be reduced to one letter words for each word length.
e.g. 2 of the 6 letter words can be reduced to one letter words
I also need to keep track of the longest word that can be reduced to a one letter word.
I am wondering if there is a better way to do this then what I have. Ill explain what I am doing
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
|
maxlength= longest word in the list;
vector<string> startingWords[maxlength]; //Original words
hash_set reduciableWords[maxlength]; //Words that reduce to size 1
//Read in all the words and store them according to their size
//Finding reducible words
for each list of starting words
for each word in the list
for each letter in the word
erase a letter //store in a temp string
check if the temp string is in the reducible words hash length-1
true: add word to reducible words hash table
add the temp string to a list for the staring word
Output stuff...
|
How can I make it better. I need to go through every list of starting words then every word in the list then every letter in the list, need to save every word that can be produces by removing one letter for output( im re-using this list for each word). I didnt psuedo code everything I am doing but just the main ideas, I also have the entire program written it just seems ugly and that it could be done better