InferenceAI

Enter at least two characters to search.

Start free

Word Ladder Length

hardGraphs40 min50 XP

Transform begin into end by changing one letter at a time, where every intermediate word must be in words. Return the number of words in the shortest sequence (including begin and end), or 0 if impossible.

Examples

EXAMPLE WALKTHROUGHclassic

A guided view of the published example, not a trace of your code.

INPUT["hit","cog",["hot","dot","dog","lot","log","cog"]]
TASKword_ladder_length()
EXPECTED OUTPUT5

Start with the input values shown in this example.

Example 1 of 3

Input: begin = "hit", end = "cog", words = ["hot","dot","dog","lot","log","cog"]

Output: 5

Explanation: classic

Input: begin = "hit", end = "cog", words = ["hot","dot","dog","lot","log"]

Output: 0

Explanation: end missing

Input: begin = "a", end = "c", words = ["a","b","c"]

Output: 2

Explanation: single letters

Hint 1

BFS over words; neighbours differ in exactly one position.

Hint 2

Precompute wildcard patterns like 'h*t' to find neighbours quickly.

Topics

DSAGraphsPython