상위 150개 인터뷰쉬움

워드래더

'Word Ladder' 문제에 대한 자세한 가이드 및 Python 구현입니다.

문제 설명

쉬움

사전 wordList를 사용하는 단어 BeginWord에서 단어 endWord로의 변환 시퀀스는 다음과 같은 단어 시퀀스인 startWord -> s1 -> s2 -> ... -> sk입니다.

- 인접한 모든 단어 쌍은 문자 하나만큼 다릅니다.

- 1 <= i <= k에 대한 모든 si는 wordList에 있습니다. BeginWord가 wordList에 있을 필요는 없습니다.

- sk == endWord.

두 단어(beginWord 및 endWord)와 사전 wordList가 주어지면 BeginWord에서 endWord까지 가장 짧은 변환 시퀀스의 단어 수를 반환하거나, 그러한 시퀀스가 ​​없으면 0을 반환합니다.

ladderLength(beginWord: str, endWord: str, wordList: List[str]) -> int 함수를 작성하세요.

제약
  • 1 <= len(beginWord) <= 10
  • endWord.length == beginWord.length
  • 1 <= len(wordList) <= 5000
  • wordList[i].length == beginWord.length
  • beginWord, endWord, and wordList[i] consist of lowercase English letters
  • All the words in wordList are unique

Example 1
Input
beginWord = "hit", endWord = "cog", wordList = ["hot","dot","dog","lot","log","cog"]
Output
5
Explanation

One shortest transformation sequence is "hit" -> "hot" -> "dot" -> "dog" -> "cog", which is 5 words long.

Example 2
Input
beginWord = "hit", endWord = "cog", wordList = ["hot","dot","dog","lot","log"]
Output
0
Explanation

The endWord "cog" is not in wordList, so there is no valid transformation sequence.

Need a Hint?
세트나 힙과 같은 그래프 관련 데이터 구조를 사용해 보세요.
Edge Cases to Watch
  • 빈 입력 구조
  • 단일 요소 입력
  • 큰 수치 범위

해결할 준비가 되셨나요?

Open the problem in PyRun's browser-based Python editor. Your code runs fully offline — no server required.

편집기에서 열기
Found this breakdown helpful?

PyRun is built and maintained by an independent solo developer. If this helped your interview prep, consider buying a coffee!

Buy me a coffee

권장 Python 리소스

관련 대화형 튜토리얼, 치트 시트, 코드 비교를 통해 지식을 확장하세요.