How do I fix my dfs implementation for word ladder problem?

Trying this leetcode problem :

I am basically doing a dfs condition. Treating each word that is at an edit distance of 1 with each other as a connected node with an edge.

However, I am not able to make my dfs exit properly because of which I am getting a run-time error. I know there are better solutions but could you please help me fix my implementation.

Here is my solution:

I really tried adding the solution here, but for some reason the formatting keeps getting screwed.

Hi, since you need to find the shortest path, shouldnt you be BFSing instead of DFSing ?

BFS Solution

Hi, the run time error was because you were returning ‘a’(which was a non-zero value) in main().

Also the value returned by dfs() in the for loop should have been stored in a variable so as to find the shortest transformation.

Here’s my solution