


START = “cow” END = “mot”
WORDDIC = [“cow”, “mow”, “cot”, “mot”]
Here answer is 2 there are two paths of distance 2 ,
“cow” -> “mow” -> “mot”
“cow” -> “cot” -> “mot”
The first line of input contains an integer 'T' denoting the number of test cases to run. Then the test case follows.
The first line of each test case contains two space-separated words START and END.
The second line of each test case contains a single integer ‘N’, denoting the length of WORDDIC.
The third line of each test case contains ‘N’ space-separated words, denoting the elements of WORDDIC.
For each test case print a single integer denoting the number of paths from START to END.
Output for each test case is printed in a separate line.
You don’t need to print anything. It has already been taken care of. Just implement the given function.
1 <= T <= 10
1 <= N <= 5000
1 <= len(WORD) <= 10
Where ‘N’ is the length of WORDDIC, len(WORD) is the length of START, END, and words in WORDDIC. All the words are lower case English words.
Time Limit: 1 sec
We will maintain two hashmaps DIST and WAYS. DIST stores the minimum distance of a word from START and ways stores the number of minimum paths from the START to the current word. To make a transition we will iterate over all positions and try all the possible characters at this position. If the new word is present in the dictionary then make the transition.
The algorithm will be: