Chaturmind
LearnDSASystem DesignDevOpsEngineering GrowthBlog
Start learning
Chaturmind

Structured learning paths for engineers who want to go deep. Written by practitioners.

Learn

  • Java
  • DSA
  • System Design
  • Spring Boot
  • AI / ML
  • DevOps
  • Engineering Growth

Company

  • Blog
  • Contact

Legal

  • Privacy Policy
  • Terms of Service

© 2026 Chaturmind. All rights reserved.

Built for engineers who want to go deep.

← Trees & Graphs

Binary Trees

  • Tree Traversal (DFS & BFS)
  • Binary Search Tree Operations
  • Practice problems

    Invert Binary Tree
  • Validate Binary Search Tree
  • Binary Tree Level Order Traversal
  • Binary Tree Inorder Traversal
  • Maximum Depth of Binary Tree
  • Binary Tree Zigzag Level Order Traversal
  • Construct Binary Tree from Preorder and Inorder Traversal
  • Insert into a Binary Search Tree
  • Kth Smallest Element in a BST
  • Lowest Common Ancestor of a Binary Tree
  • Lowest Common Ancestor of a Binary Search Tree
  • Path Sum II
  • Diameter of Binary Tree
  • Implement Trie (Prefix Tree)

Graph Algorithms

  • Graph DFS & BFS
  • Topological Sort
  • Union-Find (Disjoint Sets)
  • Practice problems

    Redundant Connection
  • Accounts Merge
  • Number of Islands
  • Clone Graph
  • Course Schedule
  • Rotting Oranges
  • Word Ladder
  • Course Schedule II
  • Number of Provinces
Chaturmind
← Trees & Graphs

Binary Trees

  • Tree Traversal (DFS & BFS)
  • Binary Search Tree Operations
  • Practice problems

    Invert Binary Tree
  • Validate Binary Search Tree
  • Binary Tree Level Order Traversal
  • Binary Tree Inorder Traversal
  • Maximum Depth of Binary Tree
  • Binary Tree Zigzag Level Order Traversal
  • Construct Binary Tree from Preorder and Inorder Traversal
  • Insert into a Binary Search Tree
  • Kth Smallest Element in a BST
  • Lowest Common Ancestor of a Binary Tree
  • Lowest Common Ancestor of a Binary Search Tree
  • Path Sum II
  • Diameter of Binary Tree
  • Implement Trie (Prefix Tree)

Graph Algorithms

  • Graph DFS & BFS
  • Topological Sort
  • Union-Find (Disjoint Sets)
  • Practice problems

    Redundant Connection
  • Accounts Merge
  • Number of Islands
  • Clone Graph
  • Course Schedule
  • Rotting Oranges
  • Word Ladder
  • Course Schedule II
  • Number of Provinces
HomeLearnTrees & GraphsGraph Algorithms
HardGraphs

Word Ladder

bfsgraphhash-set

Problem

Given a beginWord, an endWord, and a wordList, return the length of the shortest transformation sequence from beginWord to endWord, changing one letter at a time, with every intermediate word required to exist in wordList. Return 0 if no such sequence exists.

Examples

Example 1

Input: beginWord = "hit", endWord = "cog", wordList = ["hot","dot","dog","lot","log","cog"]

Output: 5

Explanation: hit -> hot -> dot -> dog -> cog, 5 words in the sequence.

Example 2

Input: beginWord = "hit", endWord = "cog", wordList = ["hot","dot","dog","lot","log"]

Output: 0

Explanation: endWord "cog" is not in wordList — no valid sequence exists.

Constraints

  • •1 <= beginWord.length <= 10
  • •endWord.length == beginWord.length
  • •1 <= wordList.length <= 5000

Hints

Hint 1

Model this as a graph problem: each word is a node, and an edge connects two words that differ by exactly one letter — then the question becomes 'shortest path from beginWord to endWord.'

Hint 2

BFS guarantees the shortest path in this unweighted graph — the same guarantee covered generally in BFS / Level Order and applied directly here.

Hint 3

Generating a word's neighbors by trying every possible single-letter substitution at every position (26 letters * word length candidates) is more efficient than comparing against every word in the list pairwise.

Solutions

// Conceptual brute force — DFS exploring every possible transformation path
public int ladderLengthDFSConceptual(String beginWord, String endWord, List<String> wordList) {
    // DFS would explore each possible one-letter-different word from the current one,
    // recursively, tracking path length, and take the minimum over ALL paths found
    // that reach endWord. This is exponential — DFS explores full paths one at a time
    // and has no way to guarantee the FIRST path it finds is the shortest one, so it
    // would need to explore every possible path before it could be sure of the minimum.
    throw new UnsupportedOperationException("Impractical — see explanation");
}

Time: Exponential in the worst case · Space: O(n * L) for the recursion

Previous · Practice problem

Rotting Oranges

Next · Practice problem

Course Schedule II