{"id":225,"date":"2025-05-31T18:51:58","date_gmt":"2025-05-31T13:21:58","guid":{"rendered":"https:\/\/codeanddebug.in\/blog\/?p=225"},"modified":"2025-05-31T18:55:59","modified_gmt":"2025-05-31T13:25:59","slug":"word-ladder-ii-leetcode-126","status":"publish","type":"post","link":"https:\/\/codeanddebug.in\/blog\/word-ladder-ii-leetcode-126\/","title":{"rendered":"Word Ladder II | Leetcode 126 | Shortest Transformation Sequences with BFS"},"content":{"rendered":"\n<p>Learn how to find all shortest paths (Word Ladder II) from one word to another by changing one letter at a time. This guide uses a clear example, and in-place code comments to illustrate a level-by-level BFS approach.<\/p>\n\n\n\n<p>Here&#8217;s the [<strong><a href=\"https:\/\/leetcode.com\/problems\/word-ladder-ii\/description\/\" target=\"_blank\" rel=\"noreferrer noopener\"><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-vivid-purple-color\"><span style=\"text-decoration: underline;\">Problem Link<\/span><\/mark><\/a><\/strong>] to begin with.<\/p>\n\n\n<div style=\"max-width: -moz-fit-content; \" class=\"wp-block-ub-table-of-contents-block ub_table-of-contents\" id=\"ub_table-of-contents-ff5f3aa9-9c83-41e3-aacc-ffd34ff03d4b\" data-linktodivider=\"false\" data-showtext=\"show\" data-hidetext=\"hide\" data-scrolltype=\"auto\" data-enablesmoothscroll=\"false\" data-initiallyhideonmobile=\"false\" data-initiallyshow=\"true\"><div class=\"ub_table-of-contents-header-container\" style=\"\">\n\t\t\t<div class=\"ub_table-of-contents-header\" style=\"text-align: left; \">\n\t\t\t\t<div class=\"ub_table-of-contents-title\">Contents:<\/div>\n\t\t\t\t<div class=\"ub_table-of-contents-header-toggle\">\n\t\t\t<div class=\"ub_table-of-contents-toggle\" style=\"\">\n\t\t\t\u00a0[<a class=\"ub_table-of-contents-toggle-link\" href=\"#\" style=\"\">hide<\/a>]\n\t\t\t<\/div>\n\t\t<\/div>\n\t\t\t<\/div>\n\t\t<\/div><div class=\"ub_table-of-contents-extra-container\" style=\"\">\n\t\t\t<div class=\"ub_table-of-contents-container ub_table-of-contents-1-column \">\n\t\t\t\t<ul style=\"\"><li style=\"\"><a href=\"https:\/\/codeanddebug.in\/blog\/word-ladder-ii-leetcode-126\/#0-what-the-problem-asks\" style=\"\">What the Problem Asks<\/a><ul><li style=\"\"><a href=\"https:\/\/codeanddebug.in\/blog\/word-ladder-ii-leetcode-126\/#1-example-1\" style=\"\">Example 1<\/a><\/li><li style=\"\"><a href=\"https:\/\/codeanddebug.in\/blog\/word-ladder-ii-leetcode-126\/#2-example-2\" style=\"\">Example 2<\/a><\/li><\/ul><\/li><li style=\"\"><a href=\"https:\/\/codeanddebug.in\/blog\/word-ladder-ii-leetcode-126\/#3-intuition-amp-approach\" style=\"\">Intuition &amp; Approach<\/a><\/li><li style=\"\"><a href=\"https:\/\/codeanddebug.in\/blog\/word-ladder-ii-leetcode-126\/#4-code\" style=\"\">Code<\/a><\/li><li style=\"\"><a href=\"https:\/\/codeanddebug.in\/blog\/word-ladder-ii-leetcode-126\/#5-how-the-code-works-step-by-step\" style=\"\">How the Code Works, Step by Step<\/a><\/li><li style=\"\"><a href=\"https:\/\/codeanddebug.in\/blog\/word-ladder-ii-leetcode-126\/#6-dry-run-with-example\" style=\"\">Dry Run with Example<\/a><\/li><li style=\"\"><a href=\"https:\/\/codeanddebug.in\/blog\/word-ladder-ii-leetcode-126\/#7-time-amp-space-complexity\" style=\"\">Time &amp; Space Complexity<\/a><\/li><li style=\"\"><a href=\"https:\/\/codeanddebug.in\/blog\/word-ladder-ii-leetcode-126\/#8-conclusion\" style=\"\">Conclusion<\/a><\/li><\/ul>\n\t\t\t<\/div>\n\t\t<\/div><\/div>\n\n\n<h2 class=\"wp-block-heading\" id=\"0-what-the-problem-asks\">What the Problem Asks<\/h2>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\">\n<p>You have a <strong>beginWord<\/strong>, an <strong>endWord<\/strong>, and a list of words called <strong>wordList<\/strong>.<br>You want to transform <code>beginWord<\/code> into <code>endWord<\/code> by changing <strong>exactly one letter<\/strong> at a time.<br>Each new intermediate word <strong>must<\/strong> be in <code>wordList<\/code>.<br>Return <strong>all possible shortest sequences<\/strong> of words from <code>beginWord<\/code> to <code>endWord<\/code>.<\/p>\n<\/blockquote>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"1-example-1\">Example 1<\/h3>\n\n\n\n<div class=\"wp-block-kevinbatdorf-code-block-pro\" data-code-block-pro-font-family=\"Code-Pro-JetBrains-Mono\" style=\"font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)\"><span style=\"display:block;padding:16px 0 0 16px;margin-bottom:-1px;width:100%;text-align:left;background-color:#1E1E1E\"><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"54\" height=\"14\" viewBox=\"0 0 54 14\"><g fill=\"none\" fill-rule=\"evenodd\" transform=\"translate(1 1)\"><circle cx=\"6\" cy=\"6\" r=\"6\" fill=\"#FF5F56\" stroke=\"#E0443E\" stroke-width=\".5\"><\/circle><circle cx=\"26\" cy=\"6\" r=\"6\" fill=\"#FFBD2E\" stroke=\"#DEA123\" stroke-width=\".5\"><\/circle><circle cx=\"46\" cy=\"6\" r=\"6\" fill=\"#27C93F\" stroke=\"#1AAB29\" stroke-width=\".5\"><\/circle><\/g><\/svg><\/span><span role=\"button\" tabindex=\"0\" data-code=\"beginWord = &quot;hit&quot;\nendWord   = &quot;cog&quot;\nwordList  = [&quot;hot&quot;,&quot;dot&quot;,&quot;dog&quot;,&quot;lot&quot;,&quot;log&quot;,&quot;cog&quot;]\n\nShortest sequences:\n  [&quot;hit&quot;,&quot;hot&quot;,&quot;dot&quot;,&quot;dog&quot;,&quot;cog&quot;]\n  [&quot;hit&quot;,&quot;hot&quot;,&quot;lot&quot;,&quot;log&quot;,&quot;cog&quot;]\" style=\"color:#D4D4D4;display:none\" aria-label=\"Copy\" class=\"code-block-pro-copy-button\"><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" style=\"width:24px;height:24px\" fill=\"none\" viewBox=\"0 0 24 24\" stroke=\"currentColor\" stroke-width=\"2\"><path class=\"with-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2m-6 9l2 2 4-4\"><\/path><path class=\"without-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2\"><\/path><\/svg><\/span><pre class=\"shiki dark-plus\" style=\"background-color: #1E1E1E\" tabindex=\"0\"><code><span class=\"line\"><span style=\"color: #D4D4D4\">beginWord = <\/span><span style=\"color: #CE9178\">&quot;hit&quot;<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">endWord   = <\/span><span style=\"color: #CE9178\">&quot;cog&quot;<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">wordList  = [<\/span><span style=\"color: #CE9178\">&quot;hot&quot;<\/span><span style=\"color: #D4D4D4\">,<\/span><span style=\"color: #CE9178\">&quot;dot&quot;<\/span><span style=\"color: #D4D4D4\">,<\/span><span style=\"color: #CE9178\">&quot;dog&quot;<\/span><span style=\"color: #D4D4D4\">,<\/span><span style=\"color: #CE9178\">&quot;lot&quot;<\/span><span style=\"color: #D4D4D4\">,<\/span><span style=\"color: #CE9178\">&quot;log&quot;<\/span><span style=\"color: #D4D4D4\">,<\/span><span style=\"color: #CE9178\">&quot;cog&quot;<\/span><span style=\"color: #D4D4D4\">]<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">Shortest sequences:<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">  [<\/span><span style=\"color: #CE9178\">&quot;hit&quot;<\/span><span style=\"color: #D4D4D4\">,<\/span><span style=\"color: #CE9178\">&quot;hot&quot;<\/span><span style=\"color: #D4D4D4\">,<\/span><span style=\"color: #CE9178\">&quot;dot&quot;<\/span><span style=\"color: #D4D4D4\">,<\/span><span style=\"color: #CE9178\">&quot;dog&quot;<\/span><span style=\"color: #D4D4D4\">,<\/span><span style=\"color: #CE9178\">&quot;cog&quot;<\/span><span style=\"color: #D4D4D4\">]<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">  [<\/span><span style=\"color: #CE9178\">&quot;hit&quot;<\/span><span style=\"color: #D4D4D4\">,<\/span><span style=\"color: #CE9178\">&quot;hot&quot;<\/span><span style=\"color: #D4D4D4\">,<\/span><span style=\"color: #CE9178\">&quot;lot&quot;<\/span><span style=\"color: #D4D4D4\">,<\/span><span style=\"color: #CE9178\">&quot;log&quot;<\/span><span style=\"color: #D4D4D4\">,<\/span><span style=\"color: #CE9178\">&quot;cog&quot;<\/span><span style=\"color: #D4D4D4\">]<\/span><\/span><\/code><\/pre><\/div>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"2-example-2\">Example 2<\/h3>\n\n\n\n<div class=\"wp-block-kevinbatdorf-code-block-pro\" data-code-block-pro-font-family=\"Code-Pro-JetBrains-Mono\" style=\"font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)\"><span style=\"display:block;padding:16px 0 0 16px;margin-bottom:-1px;width:100%;text-align:left;background-color:#1E1E1E\"><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"54\" height=\"14\" viewBox=\"0 0 54 14\"><g fill=\"none\" fill-rule=\"evenodd\" transform=\"translate(1 1)\"><circle cx=\"6\" cy=\"6\" r=\"6\" fill=\"#FF5F56\" stroke=\"#E0443E\" stroke-width=\".5\"><\/circle><circle cx=\"26\" cy=\"6\" r=\"6\" fill=\"#FFBD2E\" stroke=\"#DEA123\" stroke-width=\".5\"><\/circle><circle cx=\"46\" cy=\"6\" r=\"6\" fill=\"#27C93F\" stroke=\"#1AAB29\" stroke-width=\".5\"><\/circle><\/g><\/svg><\/span><span role=\"button\" tabindex=\"0\" data-code=\"beginWord = &quot;a&quot;\nendWord   = &quot;c&quot;\nwordList  = [&quot;a&quot;,&quot;b&quot;,&quot;c&quot;]\n\nShortest sequences:\n  [&quot;a&quot;,&quot;c&quot;]\" style=\"color:#D4D4D4;display:none\" aria-label=\"Copy\" class=\"code-block-pro-copy-button\"><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" style=\"width:24px;height:24px\" fill=\"none\" viewBox=\"0 0 24 24\" stroke=\"currentColor\" stroke-width=\"2\"><path class=\"with-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2m-6 9l2 2 4-4\"><\/path><path class=\"without-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2\"><\/path><\/svg><\/span><pre class=\"shiki dark-plus\" style=\"background-color: #1E1E1E\" tabindex=\"0\"><code><span class=\"line\"><span style=\"color: #D4D4D4\">beginWord = <\/span><span style=\"color: #CE9178\">&quot;a&quot;<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">endWord   = <\/span><span style=\"color: #CE9178\">&quot;c&quot;<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">wordList  = [<\/span><span style=\"color: #CE9178\">&quot;a&quot;<\/span><span style=\"color: #D4D4D4\">,<\/span><span style=\"color: #CE9178\">&quot;b&quot;<\/span><span style=\"color: #D4D4D4\">,<\/span><span style=\"color: #CE9178\">&quot;c&quot;<\/span><span style=\"color: #D4D4D4\">]<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">Shortest sequences:<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">  [<\/span><span style=\"color: #CE9178\">&quot;a&quot;<\/span><span style=\"color: #D4D4D4\">,<\/span><span style=\"color: #CE9178\">&quot;c&quot;<\/span><span style=\"color: #D4D4D4\">]<\/span><\/span><\/code><\/pre><\/div>\n\n\n\n<p>(\u201ca\u201d \u2192 \u201cc\u201d is valid because \u201cc\u201d is in the list, and we changed one letter.)<\/p>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\">\n<p><em>Also read about the Python Program to <strong><a href=\"https:\/\/codeanddebug.in\/blog\/python-program-print-divisors-factors-integer\/\" target=\"_blank\" rel=\"noreferrer noopener\"><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-vivid-purple-color\"><span style=\"text-decoration: underline;\">Solve Word Ladder &#8211; 1<\/span><\/mark><\/a><\/strong><\/em>.<\/p>\n<\/blockquote>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"3-intuition-amp-approach\">Intuition &amp; Approach<\/h2>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>We want all shortest sequences<\/strong>, not just one.<\/li>\n\n\n\n<li><strong>BFS layer by layer<\/strong> naturally finds the shortest distance.<\/li>\n\n\n\n<li>At each step, we keep whole sequences in the queue (not just words).<\/li>\n\n\n\n<li>We also remove words from the set once they appear at the current level, so later levels don\u2019t reuse them and produce longer paths.<\/li>\n<\/ol>\n\n\n\n<p><strong>In very simple terms:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Start with a queue of lists, each list is a transformation path so far.<\/li>\n\n\n\n<li>In each iteration, we pop a path, look at its last word, generate all possible \u201cone-letter\u201d changes.<\/li>\n\n\n\n<li>If a changed word is in <code>wordSet<\/code>, we make a <strong>new path<\/strong> by appending that word and push it into the queue.<\/li>\n\n\n\n<li>As soon as we see a path ending in <code>endWord<\/code>, we record it\u2014because BFS guarantees this is the shortest length.<\/li>\n\n\n\n<li>We keep collecting all such paths until we finish processing the current BFS layer.<\/li>\n\n\n\n<li>Finally, we return all collected shortest paths.<\/li>\n<\/ul>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"4-code\">Code<\/h2>\n\n\n\n<div class=\"wp-block-kevinbatdorf-code-block-pro\" data-code-block-pro-font-family=\"Code-Pro-JetBrains-Mono\" style=\"font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)\"><span style=\"display:block;padding:16px 0 0 16px;margin-bottom:-1px;width:100%;text-align:left;background-color:#1E1E1E\"><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"54\" height=\"14\" viewBox=\"0 0 54 14\"><g fill=\"none\" fill-rule=\"evenodd\" transform=\"translate(1 1)\"><circle cx=\"6\" cy=\"6\" r=\"6\" fill=\"#FF5F56\" stroke=\"#E0443E\" stroke-width=\".5\"><\/circle><circle cx=\"26\" cy=\"6\" r=\"6\" fill=\"#FFBD2E\" stroke=\"#DEA123\" stroke-width=\".5\"><\/circle><circle cx=\"46\" cy=\"6\" r=\"6\" fill=\"#27C93F\" stroke=\"#1AAB29\" stroke-width=\".5\"><\/circle><\/g><\/svg><\/span><span role=\"button\" tabindex=\"0\" data-code=\"from collections import deque\n\nclass Solution:\n    def findLadders(\n        self, beginWord: str, endWord: str, wordList: List[str]\n    ) -&gt; List[List[str]]:\n        # 1. Put all words into a set for fast lookups\n        wordSet = set(wordList)\n        # If the endWord is not in our list, we can\u2019t transform to it\n        if endWord not in wordSet:\n            return []\n\n        result = []              # To store all shortest sequences\n        queue = deque()          # BFS queue of paths (lists of words)\n        queue.append([beginWord])  # Start with the path [&quot;beginWord&quot;]\n\n        while len(queue) != 0:\n            level_size = len(queue)\n            chosen_words = set()  # Words we will remove after this layer\n\n            # Process everything in the current layer\n            for _ in range(level_size):\n                sequence = queue.popleft()   # A path so far\n                last_word = sequence[-1]      # The most recent word\n\n                # If we already reached endWord, record this path\n                if last_word == endWord:\n                    result.append(sequence)\n                    # Don\u2019t continue expanding this path; it\u2019s complete\n                    continue\n\n                # Try changing each letter of last_word\n                for i in range(len(last_word)):\n                    for ch in &quot;abcdefghijklmnopqrstuvwxyz&quot;:\n                        # Skip if the letter is the same\n                        if ch == last_word[i]:\n                            continue\n                        # Form a new word by replacing the i-th letter\n                        new_word = last_word[:i] + ch + last_word[i + 1:]\n                        # If this new word is in our set, it\u2019s valid\n                        if new_word in wordSet:\n                            # Make a new path by appending new_word\n                            new_seq = sequence + [new_word]\n                            queue.append(new_seq)\n                            # Mark new_word for removal after this layer\n                            chosen_words.add(new_word)\n\n            # Remove all words that were used in this layer\n            for w in chosen_words:\n                wordSet.remove(w)\n\n        return result\" style=\"color:#D4D4D4;display:none\" aria-label=\"Copy\" class=\"code-block-pro-copy-button\"><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" style=\"width:24px;height:24px\" fill=\"none\" viewBox=\"0 0 24 24\" stroke=\"currentColor\" stroke-width=\"2\"><path class=\"with-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2m-6 9l2 2 4-4\"><\/path><path class=\"without-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2\"><\/path><\/svg><\/span><pre class=\"shiki dark-plus\" style=\"background-color: #1E1E1E\" tabindex=\"0\"><code><span class=\"line\"><span style=\"color: #C586C0\">from<\/span><span style=\"color: #D4D4D4\"> collections <\/span><span style=\"color: #C586C0\">import<\/span><span style=\"color: #D4D4D4\"> deque<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #569CD6\">class<\/span><span style=\"color: #D4D4D4\"> <\/span><span style=\"color: #4EC9B0\">Solution<\/span><span style=\"color: #D4D4D4\">:<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">    <\/span><span style=\"color: #569CD6\">def<\/span><span style=\"color: #D4D4D4\"> <\/span><span style=\"color: #DCDCAA\">findLadders<\/span><span style=\"color: #D4D4D4\">(<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">        <\/span><span style=\"color: #9CDCFE\">self<\/span><span style=\"color: #D4D4D4\">, <\/span><span style=\"color: #9CDCFE\">beginWord<\/span><span style=\"color: #D4D4D4\">: <\/span><span style=\"color: #4EC9B0\">str<\/span><span style=\"color: #D4D4D4\">, <\/span><span style=\"color: #9CDCFE\">endWord<\/span><span style=\"color: #D4D4D4\">: <\/span><span style=\"color: #4EC9B0\">str<\/span><span style=\"color: #D4D4D4\">, <\/span><span style=\"color: #9CDCFE\">wordList<\/span><span style=\"color: #D4D4D4\">: List[<\/span><span style=\"color: #4EC9B0\">str<\/span><span style=\"color: #D4D4D4\">]<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">    ) -&gt; List[List[<\/span><span style=\"color: #4EC9B0\">str<\/span><span style=\"color: #D4D4D4\">]]:<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">        <\/span><span style=\"color: #6A9955\"># 1. Put all words into a set for fast lookups<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">        wordSet = <\/span><span style=\"color: #4EC9B0\">set<\/span><span style=\"color: #D4D4D4\">(wordList)<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">        <\/span><span style=\"color: #6A9955\"># If the endWord is not in our list, we can\u2019t transform to it<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">        <\/span><span style=\"color: #C586C0\">if<\/span><span style=\"color: #D4D4D4\"> endWord <\/span><span style=\"color: #569CD6\">not<\/span><span style=\"color: #D4D4D4\"> <\/span><span style=\"color: #569CD6\">in<\/span><span style=\"color: #D4D4D4\"> wordSet:<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">            <\/span><span style=\"color: #C586C0\">return<\/span><span style=\"color: #D4D4D4\"> []<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">        result = []              <\/span><span style=\"color: #6A9955\"># To store all shortest sequences<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">        queue = deque()          <\/span><span style=\"color: #6A9955\"># BFS queue of paths (lists of words)<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">        queue.append([beginWord])  <\/span><span style=\"color: #6A9955\"># Start with the path [&quot;beginWord&quot;]<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">        <\/span><span style=\"color: #C586C0\">while<\/span><span style=\"color: #D4D4D4\"> <\/span><span style=\"color: #DCDCAA\">len<\/span><span style=\"color: #D4D4D4\">(queue) != <\/span><span style=\"color: #B5CEA8\">0<\/span><span style=\"color: #D4D4D4\">:<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">            level_size = <\/span><span style=\"color: #DCDCAA\">len<\/span><span style=\"color: #D4D4D4\">(queue)<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">            chosen_words = <\/span><span style=\"color: #4EC9B0\">set<\/span><span style=\"color: #D4D4D4\">()  <\/span><span style=\"color: #6A9955\"># Words we will remove after this layer<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">            <\/span><span style=\"color: #6A9955\"># Process everything in the current layer<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">            <\/span><span style=\"color: #C586C0\">for<\/span><span style=\"color: #D4D4D4\"> _ <\/span><span style=\"color: #C586C0\">in<\/span><span style=\"color: #D4D4D4\"> <\/span><span style=\"color: #DCDCAA\">range<\/span><span style=\"color: #D4D4D4\">(level_size):<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">                sequence = queue.popleft()   <\/span><span style=\"color: #6A9955\"># A path so far<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">                last_word = sequence[-<\/span><span style=\"color: #B5CEA8\">1<\/span><span style=\"color: #D4D4D4\">]      <\/span><span style=\"color: #6A9955\"># The most recent word<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">                <\/span><span style=\"color: #6A9955\"># If we already reached endWord, record this path<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">                <\/span><span style=\"color: #C586C0\">if<\/span><span style=\"color: #D4D4D4\"> last_word == endWord:<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">                    result.append(sequence)<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">                    <\/span><span style=\"color: #6A9955\"># Don\u2019t continue expanding this path; it\u2019s complete<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">                    <\/span><span style=\"color: #C586C0\">continue<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">                <\/span><span style=\"color: #6A9955\"># Try changing each letter of last_word<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">                <\/span><span style=\"color: #C586C0\">for<\/span><span style=\"color: #D4D4D4\"> i <\/span><span style=\"color: #C586C0\">in<\/span><span style=\"color: #D4D4D4\"> <\/span><span style=\"color: #DCDCAA\">range<\/span><span style=\"color: #D4D4D4\">(<\/span><span style=\"color: #DCDCAA\">len<\/span><span style=\"color: #D4D4D4\">(last_word)):<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">                    <\/span><span style=\"color: #C586C0\">for<\/span><span style=\"color: #D4D4D4\"> ch <\/span><span style=\"color: #C586C0\">in<\/span><span style=\"color: #D4D4D4\"> <\/span><span style=\"color: #CE9178\">&quot;abcdefghijklmnopqrstuvwxyz&quot;<\/span><span style=\"color: #D4D4D4\">:<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">                        <\/span><span style=\"color: #6A9955\"># Skip if the letter is the same<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">                        <\/span><span style=\"color: #C586C0\">if<\/span><span style=\"color: #D4D4D4\"> ch == last_word[i]:<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">                            <\/span><span style=\"color: #C586C0\">continue<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">                        <\/span><span style=\"color: #6A9955\"># Form a new word by replacing the i-th letter<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">                        new_word = last_word[:i] + ch + last_word[i + <\/span><span style=\"color: #B5CEA8\">1<\/span><span style=\"color: #D4D4D4\">:]<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">                        <\/span><span style=\"color: #6A9955\"># If this new word is in our set, it\u2019s valid<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">                        <\/span><span style=\"color: #C586C0\">if<\/span><span style=\"color: #D4D4D4\"> new_word <\/span><span style=\"color: #569CD6\">in<\/span><span style=\"color: #D4D4D4\"> wordSet:<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">                            <\/span><span style=\"color: #6A9955\"># Make a new path by appending new_word<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">                            new_seq = sequence + [new_word]<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">                            queue.append(new_seq)<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">                            <\/span><span style=\"color: #6A9955\"># Mark new_word for removal after this layer<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">                            chosen_words.add(new_word)<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">            <\/span><span style=\"color: #6A9955\"># Remove all words that were used in this layer<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">            <\/span><span style=\"color: #C586C0\">for<\/span><span style=\"color: #D4D4D4\"> w <\/span><span style=\"color: #C586C0\">in<\/span><span style=\"color: #D4D4D4\"> chosen_words:<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">                wordSet.remove(w)<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">        <\/span><span style=\"color: #C586C0\">return<\/span><span style=\"color: #D4D4D4\"> result<\/span><\/span><\/code><\/pre><\/div>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"5-how-the-code-works-step-by-step\">How the Code Works, Step by Step<\/h2>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>wordSet = set(wordList)<\/strong>\n<ul class=\"wp-block-list\">\n<li>We need to quickly check if a candidate word is allowed. A Python set does that in O(1).<\/li>\n<\/ul>\n<\/li>\n\n\n\n<li><strong>If endWord not in wordSet, return []<\/strong>\n<ul class=\"wp-block-list\">\n<li>If you can\u2019t ever reach the target word, answer is empty.<\/li>\n<\/ul>\n<\/li>\n\n\n\n<li><strong>Initialize<\/strong>\n<ul class=\"wp-block-list\">\n<li><code>result = []<\/code> will hold all shortest paths we find.<\/li>\n\n\n\n<li><code>queue<\/code> holds paths. Start with just <code>[beginWord]<\/code>.<\/li>\n<\/ul>\n<\/li>\n\n\n\n<li><strong>While queue is not empty<\/strong>\n<ul class=\"wp-block-list\">\n<li><code>level_size = len(queue)<\/code> captures how many paths are in this BFS level.<\/li>\n\n\n\n<li><code>chosen_words = set()<\/code> to track which new words we used in this level.<\/li>\n<\/ul>\n<\/li>\n\n\n\n<li><strong>Process each path in the current BFS level<\/strong>\n<ul class=\"wp-block-list\">\n<li><code>sequence = queue.popleft()<\/code>: get the next path.<\/li>\n\n\n\n<li><code>last_word = sequence[-1]<\/code>: look at the last word in that path.<\/li>\n\n\n\n<li>If <code>last_word == endWord<\/code>, we found a complete shortest path. Append it to <code>result<\/code> and do not extend it further (because BFS guarantees this is minimal length).<\/li>\n\n\n\n<li>Otherwise, <strong>generate neighbors<\/strong> by changing one letter at a time:\n<ul class=\"wp-block-list\">\n<li>For each position <code>i<\/code> in <code>last_word<\/code>, try every letter <code>a\u2013z<\/code>.<\/li>\n\n\n\n<li>Skip if the letter is unchanged.<\/li>\n\n\n\n<li>Form <code>new_word<\/code>.<\/li>\n\n\n\n<li>If <code>new_word<\/code> is in <code>wordSet<\/code>, it\u2019s a valid next step:\n<ul class=\"wp-block-list\">\n<li><code>new_seq = sequence + [new_word]<\/code> builds a new path list.<\/li>\n\n\n\n<li>Enqueue <code>new_seq<\/code>.<\/li>\n\n\n\n<li>Add <code>new_word<\/code> to <code>chosen_words<\/code> so that at the end of this layer we remove it from <code>wordSet<\/code>.<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<\/li>\n\n\n\n<li><strong>After finishing this entire BFS layer<\/strong>, remove all <code>chosen_words<\/code> from <code>wordSet<\/code>.\n<ul class=\"wp-block-list\">\n<li>This prevents paths in deeper layers (longer paths) from reusing the same words and ensures we only collect shortest sequences.<\/li>\n<\/ul>\n<\/li>\n\n\n\n<li><strong>Return <code>result<\/code><\/strong>\n<ul class=\"wp-block-list\">\n<li>All recorded paths in <code>result<\/code> have length equal to the shortest transformation count.<\/li>\n<\/ul>\n<\/li>\n<\/ol>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"6-dry-run-with-example\">Dry Run with Example<\/h2>\n\n\n\n<div class=\"wp-block-kevinbatdorf-code-block-pro\" data-code-block-pro-font-family=\"Code-Pro-JetBrains-Mono\" style=\"font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)\"><span style=\"display:block;padding:16px 0 0 16px;margin-bottom:-1px;width:100%;text-align:left;background-color:#1E1E1E\"><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"54\" height=\"14\" viewBox=\"0 0 54 14\"><g fill=\"none\" fill-rule=\"evenodd\" transform=\"translate(1 1)\"><circle cx=\"6\" cy=\"6\" r=\"6\" fill=\"#FF5F56\" stroke=\"#E0443E\" stroke-width=\".5\"><\/circle><circle cx=\"26\" cy=\"6\" r=\"6\" fill=\"#FFBD2E\" stroke=\"#DEA123\" stroke-width=\".5\"><\/circle><circle cx=\"46\" cy=\"6\" r=\"6\" fill=\"#27C93F\" stroke=\"#1AAB29\" stroke-width=\".5\"><\/circle><\/g><\/svg><\/span><span role=\"button\" tabindex=\"0\" data-code=\"beginWord = &quot;hit&quot;\nendWord   = &quot;cog&quot;\nwordList  = [&quot;hot&quot;,&quot;dot&quot;,&quot;dog&quot;,&quot;lot&quot;,&quot;log&quot;,&quot;cog&quot;]\" style=\"color:#D4D4D4;display:none\" aria-label=\"Copy\" class=\"code-block-pro-copy-button\"><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" style=\"width:24px;height:24px\" fill=\"none\" viewBox=\"0 0 24 24\" stroke=\"currentColor\" stroke-width=\"2\"><path class=\"with-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2m-6 9l2 2 4-4\"><\/path><path class=\"without-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2\"><\/path><\/svg><\/span><pre class=\"shiki dark-plus\" style=\"background-color: #1E1E1E\" tabindex=\"0\"><code><span class=\"line\"><span style=\"color: #D4D4D4\">beginWord = <\/span><span style=\"color: #CE9178\">&quot;hit&quot;<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">endWord   = <\/span><span style=\"color: #CE9178\">&quot;cog&quot;<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">wordList  = [<\/span><span style=\"color: #CE9178\">&quot;hot&quot;<\/span><span style=\"color: #D4D4D4\">,<\/span><span style=\"color: #CE9178\">&quot;dot&quot;<\/span><span style=\"color: #D4D4D4\">,<\/span><span style=\"color: #CE9178\">&quot;dog&quot;<\/span><span style=\"color: #D4D4D4\">,<\/span><span style=\"color: #CE9178\">&quot;lot&quot;<\/span><span style=\"color: #D4D4D4\">,<\/span><span style=\"color: #CE9178\">&quot;log&quot;<\/span><span style=\"color: #D4D4D4\">,<\/span><span style=\"color: #CE9178\">&quot;cog&quot;<\/span><span style=\"color: #D4D4D4\">]<\/span><\/span><\/code><\/pre><\/div>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>Initialize:<\/strong>\n<ul class=\"wp-block-list\">\n<li><code>wordSet = {\"hot\",\"dot\",\"dog\",\"lot\",\"log\",\"cog\"}<\/code><\/li>\n\n\n\n<li><code>queue = [[\"hit\"]]<\/code>.<\/li>\n<\/ul>\n<\/li>\n\n\n\n<li><strong>Layer 1 (size=1):<\/strong>\n<ul class=\"wp-block-list\">\n<li>Sequence: <code>[\"hit\"]<\/code>, last_word=&#8221;hit&#8221;<\/li>\n\n\n\n<li>Generate neighbors of &#8220;hit&#8221;:\n<ul class=\"wp-block-list\">\n<li>Change \u201ch\u201d\u2192\u201ca\u201d..\u201cz\u201d except \u201ch\u201d; only valid is &#8220;hot&#8221;.<\/li>\n<\/ul>\n<\/li>\n\n\n\n<li>New path <code>[\"hit\",\"hot\"]<\/code>, queue it.<\/li>\n\n\n\n<li><code>chosen_words = {\"hot\"}<\/code>.<\/li>\n\n\n\n<li>End layer, remove &#8220;hot&#8221; from <code>wordSet<\/code>.<\/li>\n<\/ul>\n<\/li>\n\n\n\n<li><strong>Layer 2 (size=1):<\/strong>\n<ul class=\"wp-block-list\">\n<li>Sequence: <code>[\"hit\",\"hot\"]<\/code>, last_word=&#8221;hot&#8221;<\/li>\n\n\n\n<li>Generate neighbors for &#8220;hot&#8221;:\n<ul class=\"wp-block-list\">\n<li>&#8220;dot&#8221; and &#8220;lot&#8221; are valid (in <code>wordSet<\/code>).<\/li>\n<\/ul>\n<\/li>\n\n\n\n<li>Enqueue <code>[\"hit\",\"hot\",\"dot\"]<\/code> and <code>[\"hit\",\"hot\",\"lot\"]<\/code>.<\/li>\n\n\n\n<li><code>chosen_words = {\"dot\",\"lot\"}<\/code>.<\/li>\n\n\n\n<li>Remove them from <code>wordSet<\/code>.<\/li>\n<\/ul>\n<\/li>\n\n\n\n<li><strong>Layer 3 (size=2):<\/strong>\n<ul class=\"wp-block-list\">\n<li>Sequence <code>[\"hit\",\"hot\",\"dot\"]<\/code>, last_word=&#8221;dot&#8221;\n<ul class=\"wp-block-list\">\n<li>Valid neighbor: &#8220;dog&#8221; \u2192 enqueue <code>[\"hit\",\"hot\",\"dot\",\"dog\"]<\/code>.<\/li>\n<\/ul>\n<\/li>\n\n\n\n<li>Sequence <code>[\"hit\",\"hot\",\"lot\"]<\/code>, last_word=&#8221;lot&#8221;\n<ul class=\"wp-block-list\">\n<li>Valid neighbor: &#8220;log&#8221; \u2192 enqueue <code>[\"hit\",\"hot\",\"lot\",\"log\"]<\/code>.<\/li>\n<\/ul>\n<\/li>\n\n\n\n<li><code>chosen_words = {\"dog\",\"log\"}<\/code>.<\/li>\n\n\n\n<li>Remove \u201cdog\u201d and \u201clog\u201d from <code>wordSet<\/code>.<\/li>\n<\/ul>\n<\/li>\n\n\n\n<li><strong>Layer 4 (size=2):<\/strong>\n<ul class=\"wp-block-list\">\n<li>Sequence <code>[\"hit\",\"hot\",\"dot\",\"dog\"]<\/code>, last_word=&#8221;dog&#8221;\n<ul class=\"wp-block-list\">\n<li>Valid neighbor: &#8220;cog&#8221; \u2192 enqueue <code>[\"hit\",\"hot\",\"dot\",\"dog\",\"cog\"]<\/code>.<\/li>\n<\/ul>\n<\/li>\n\n\n\n<li>Sequence <code>[\"hit\",\"hot\",\"lot\",\"log\"]<\/code>, last_word=&#8221;log&#8221;\n<ul class=\"wp-block-list\">\n<li>Valid neighbor: &#8220;cog&#8221; \u2192 enqueue <code>[\"hit\",\"hot\",\"lot\",\"log\",\"cog\"]<\/code>.<\/li>\n<\/ul>\n<\/li>\n\n\n\n<li><code>chosen_words = {\"cog\"}<\/code>.<\/li>\n\n\n\n<li>Remove \u201ccog\u201d from <code>wordSet<\/code>.<\/li>\n<\/ul>\n<\/li>\n\n\n\n<li><strong>Layer 5 (size=2):<\/strong>\n<ul class=\"wp-block-list\">\n<li>Sequence <code>[\"hit\",\"hot\",\"dot\",\"dog\",\"cog\"]<\/code>, last_word=&#8221;cog&#8221; \u2192 matches <code>endWord<\/code>, record it in <code>result<\/code>.<\/li>\n\n\n\n<li>Sequence <code>[\"hit\",\"hot\",\"lot\",\"log\",\"cog\"]<\/code>, last_word=&#8221;cog&#8221; \u2192 record it in <code>result<\/code>.<\/li>\n<\/ul>\n<\/li>\n\n\n\n<li><strong>Return<\/strong>\n<ul class=\"wp-block-list\">\n<li><code>result = [ [\"hit\",\"hot\",\"dot\",\"dog\",\"cog\"], [\"hit\",\"hot\",\"lot\",\"log\",\"cog\"] ]<\/code>.<\/li>\n<\/ul>\n<\/li>\n<\/ol>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"7-time-amp-space-complexity\">Time &amp; Space Complexity<\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>Time Complexity:<\/strong>\n<ul class=\"wp-block-list\">\n<li>BFS can enqueue up to O(N \u00d7 L) paths in the worst case, where N = number of words and L = word length.<\/li>\n\n\n\n<li>Each path expansion tries L positions \u00d7 26 letters \u2192 O(26 \u00d7 L) checks per path.<\/li>\n\n\n\n<li>Overall roughly O(N \u00d7 L \u00d7 26), which is acceptable for moderate input sizes.<\/li>\n<\/ul>\n<\/li>\n\n\n\n<li><strong>Space Complexity:<\/strong>\n<ul class=\"wp-block-list\">\n<li>Storing all partial paths and a word set \u2192 O(N \u00d7 L) in total.<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"8-conclusion\">Conclusion<\/h2>\n\n\n\n<p>By using a <strong>level-by-level BFS<\/strong> where each queue entry is a full transformation path, we automatically find the shortest sequences. Removing each new word from the set at the end of its layer ensures <strong>we never revisit<\/strong> longer paths. This simple method collects all shortest paths in a clean, easy-to-follow way. Happy coding!<\/p>\n\n\n\n<div class=\"wp-block-buttons is-content-justification-center is-layout-flex wp-container-core-buttons-is-layout-16018d1d wp-block-buttons-is-layout-flex\">\n<div class=\"wp-block-button\"><a class=\"wp-block-button__link wp-element-button\" href=\"https:\/\/www.codeanddebug.in\/course\/zero-to-hero-python-dsa\" target=\"_blank\" rel=\"noreferrer noopener\">Join our Advance DSA COURSE<\/a><\/div>\n<\/div>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<p><em>For any changes to the article, kindly email at <a href=\"mailto:code@codeanddebug.in\">code@codeanddebug.in<\/a> or contact us at <a href=\"tel:+91-9712928220\">+91-9712928220<\/a>.<\/em><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Learn how to find all shortest paths (Word Ladder II) from one word to another by changing one letter at a time. This guide uses a clear example, and in-place code comments to illustrate a level-by-level BFS approach. Here&#8217;s the [Problem Link] to begin with. What the Problem Asks You have a beginWord, an endWord,<\/p>\n","protected":false},"author":1,"featured_media":227,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[3,5],"tags":[17,18],"class_list":{"0":"post-225","1":"post","2":"type-post","3":"status-publish","4":"format-standard","5":"has-post-thumbnail","7":"category-data-structures-and-algorithm","8":"category-expert","9":"tag-graph","10":"tag-hard"},"featured_image_src":"https:\/\/codeanddebug.in\/blog\/wp-content\/uploads\/2025\/05\/word-ladder-2-featured-image.png","author_info":{"display_name":"codeanddebug","author_link":"https:\/\/codeanddebug.in\/blog\/author\/codeanddebug\/"},"_links":{"self":[{"href":"https:\/\/codeanddebug.in\/blog\/wp-json\/wp\/v2\/posts\/225","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/codeanddebug.in\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/codeanddebug.in\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/codeanddebug.in\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/codeanddebug.in\/blog\/wp-json\/wp\/v2\/comments?post=225"}],"version-history":[{"count":3,"href":"https:\/\/codeanddebug.in\/blog\/wp-json\/wp\/v2\/posts\/225\/revisions"}],"predecessor-version":[{"id":236,"href":"https:\/\/codeanddebug.in\/blog\/wp-json\/wp\/v2\/posts\/225\/revisions\/236"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/codeanddebug.in\/blog\/wp-json\/wp\/v2\/media\/227"}],"wp:attachment":[{"href":"https:\/\/codeanddebug.in\/blog\/wp-json\/wp\/v2\/media?parent=225"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/codeanddebug.in\/blog\/wp-json\/wp\/v2\/categories?post=225"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/codeanddebug.in\/blog\/wp-json\/wp\/v2\/tags?post=225"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}