Question 2 Word Ladder I

Method 1 DFS backtracking

Method 2 BFS

High level

  • this is a graph problem,

    • vertex:每个单词就是一个点

    • edge:在wordList里面和自己只相差一个字典的单词都是neighbor

  • therefore, this problem is asking about the shortest path problem

    • from start node to target node

    • unit weight shortest path problem

    • method BFS

  • High level of BFS

    • Expansion: 每一次我从Queue里poll处当前层需要expand(visit)的node

    • Generation:得有办法找到wordList里所有和当前层expand的word相差一个字的

Details

  • generation这一步怎么做?

Method 1: Brute Force

Method 2 自己探索隐藏的图

  • 直接从单词本身出发==》 从Node本身出发去找neighbor

  • 遍历这个单词,尝试替换每个单词中的每个字母,看看这个单词在不在wordList里面

Clarification && Assumption:

  • StartWord 和 EndWord一样怎么办

  • EndWord or StartWord 不在wordList里怎么办

  • 到不了return什么?

Modeling Graph

Last updated