← 返回 snapchat 的题目列表Word Ladder Reachability (Boolean)
类型:online_judge
Problem
Given two equal-length strings beginWord and endWord, and a list of words wordList (array of strings). You may perform a sequence of transformations:
In one transformation, you may replace exactly one character in the current word with another lowercase English letter.
The resulting word must exist in wordList.
Return whether there exists a sequence of transformations from beginWord to endWord (0 or more steps). Return true if possible, otherwise false.
Note: You do not need to compute the shortest length or return the path.
I/O format (for stdin/stdout)
Read from stdin:
Line 1: beginWord
Line 2: endWord
Line 3: integer n, the length of wordList
Next n lines: one word per line, forming wordList
Write to stdout:
true or false
Constraints
1 <= len(beginWord) = len(endWord) <= 20
1 <= n <= 5000
Each wordList[i] contains only lowercase letters a-z and has the same length as beginWord
All words in wordList are unique
Examples
beginWord = "hit", endWord = "cog", wordList = ["hot","dot","dog","lot","log","cog"] => true
beginWord = "hit", endWord = "cog", wordList = ["hot","dot","dog","lot","log"] => false
Task
Print whether endWord is reachable from beginWord under the rules.
Example
Input
hit
cog
6
hot
dot
dog
lot
log
cog
Output
true