Shortest path to transform one word into another

后端 未结 9 2372
梦如初夏
梦如初夏 2020-12-01 00:44

For a Data Structures project, I must find the shortest path between two words (like \"cat\" and \"dog\"), changing only one letter at a time. We a

9条回答
  •  生来不讨喜
    2020-12-01 00:54

    NEW ANSWER

    Given the recent update, you could try A* with the Hamming distance as a heuristic. It's an admissible heuristic since it's not going to overestimate the distance

    OLD ANSWER

    You can modify the dynamic-program used to compute the Levenshtein distance to obtain the sequence of operations.

    EDIT: If there are a constant number of strings, the problem is solvable in polynomial time. Else, it's NP-hard (it's all there in wikipedia) .. assuming your friend is talking about the problem being NP-hard.

    EDIT: If your strings are of equal length, you can use Hamming distance.

提交回复
热议问题