[NeetCode 150] Word Ladder

16 篇文章 0 订阅

Word Ladder

You are given two words, beginWord and endWord, and also a list of words wordList. All of the given words are of the same length, consisting of lowercase English letters, and are all distinct.

Your goal is to transform beginWord into endWord by following the rules:

You may transform beginWord to any word within wordList, provided that at exactly one position the words have a different character, and the rest of the positions have the same characters.
You may repeat the previous step with the new word that you obtain, and you may do this as many times as needed.
Return the minimum number of words within the transformation sequence needed to obtain the endWord, or 0 if no such sequence exists.

Example 1:

Input: beginWord = "cat", endWord = "sag", wordList = ["bat","bag","sag","dag","dot"]

Output: 4

Explanation: The transformation sequence is “cat” -> “bat” -> “bag” -> “sag”.

Example 2:

Input: beginWord = "cat", endWord = "sag", wordList = ["bat","bag","sat","dag","dot"]

Output: 0

Explanation: There is no possible transformation sequence from “cat” to “sag” since the word “sag” is not in the wordList.

Constraints:

1 <= beginWord.length <= 10
1 <= wordList.length <= 100

Solution

The “distance” of every transformation is 1 so it is OK to apply BFS for searching the shortest path. Because it will take O ( wordList.length 2 × beginWord.length 2 ) O(\text{wordList.length}^2\times\text{beginWord.length}^2) O(wordList.length2×beginWord.length2) to build up the graph inevitably, more advanced shortest path algorithm is not necessary.

At first, we put begin word into BFS queue and set the initial distance of 1. Then we keep getting the top word from queue and check whether it can reach out other unvisited words. If so, we add these new words into queue and set their corresponding distance to current distance+1. When we reach the end word during this process, we can return early. If we cannot reach end word after the queue is empty, it means the end word is not reachable.

Code

class Solution:
    def ladderLength(self, beginWord: str, endWord: str, wordList: List[str]) -> int:
        if beginWord == endWord:
            return 1
        vis_flag = {word: False for word in wordList}
        # dis = {word: 10000000 for word in wordList}
        vis_flag[beginWord] = True
        vis_flag[endWord] = False
        from queue import Queue
        bfs_queue = Queue()
        bfs_queue.put((beginWord, 1))
        def check(a, b):
            if a == b:
                return False
            cnt = 0
            for i in range(len(a)):
                if a[i] != b[i]:
                    cnt += 1
            return cnt == 1
        while not bfs_queue.empty():
            cur = bfs_queue.get()
            for word in wordList:
                if not vis_flag[word] and check(cur[0], word):
                    if word == endWord:
                        return cur[1] + 1
                    vis_flag[word] = True
                    bfs_queue.put((word, cur[1]+1))
        return 0
        
  • 18
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
基恩士(Kinco)的ladder builder是一款用于PLC(可编程逻辑控制器)编程的软件工具。PLC是一种用于自动化控制的硬件设备,常用于工业领域中的各种控制系统。 ladder builder是基恩士公司为了方便用户进行PLC编程开发的工具软件。它提供了直观、简单的梯形图编程界面,使用户能够以图形化的方式编写和编辑PLC程序。通过该软件,用户可以使用梯形图的元素(如继电器、计数器、定时器等)来构建逻辑控制程序,实现对机器、设备或生产线的自动化控制。 使用ladder builder进行PLC编程的优点是它的易学性和易用性。用户无需具备高级的编程知识,只需掌握梯形图的基本原理即可。软件提供了丰富的模块和函数库,用户可以根据实际需要选择和配置,以满足各种控制需求。此外,ladder builder还支持多种编程语言,如ST(结构化文本)、FBD(功能块图)、SFC(顺序功能图)等,使得用户可以根据自己的喜好和需求选择适合自己的编程方式。 另外,ladder builder还具有调试和仿真功能,用户可以在软件中进行程序的调试和测试,以确保程序的正确性和可靠性。通过仿真功能,用户可以在实际硬件设备之前对程序进行模拟,以检查程序的逻辑是否正确,避免出现错误和故障。这大大节省了用户的时间和成本。 基恩士的ladder builder软件是一款功能强大、易于使用的PLC编程工具,它提供了丰富的功能和模块,使用户能够以图形化的方式轻松编写和编辑PLC程序,实现对机器和设备的自动化控制。无论是初学者还是经验丰富的工程师,都可以通过ladder builder来提高PLC编程的效率和质量。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

ShadyPi

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值