Word ladder II

Graphs Hard Problems Hard
  • This type of problem is central in applications such as machine translation, autocorrect algorithms, or even text-based games
  • The ability to transform one word into another by changing letters, while still producing valid words, forms the foundation of such systems
  • The concept is known as Levenshtein distance or Edit distance in computer science, which measures the minimum number of operations (transformations) required to change one word into another
  • It is a popular algorithm in Natural Language Processing and Computational Linguistics

Given two distinct words startWord and targetWord, and a list denoting wordList of unique words of equal lengths. Find all shortest transformation sequence(s) from startWord to targetWord. You can return them in any order possible.


In this problem statement, we need to keep the following conditions in mind:


A word can only consist of lowercase characters.

Only one letter can be changed in each transformation.

Each transformed word must exist in the wordList including the targetWord.

startWord may or may not be part of the wordList.

Return an empty list if there is no such transformation sequence.

Examples:

Input: startWord = "der", targetWord = "dfs", wordList = ["des", "der", "dfr", "dgt", "dfs"]


Output: [ [ “der”, “dfr”, “dfs” ], [ “der”, “des”, “dfs”] ]


Explanation: The length of the smallest transformation sequence here is 3.

Following are the only two shortest ways to get to the targetWord from the startWord :

"der" -> ( replace ‘r’ by ‘s’ ) -> "des" -> ( replace ‘e’ by ‘f’ ) -> "dfs".

"der" -> ( replace ‘e’ by ‘f’ ) -> "dfr" -> ( replace ‘r’ by ‘s’ ) -> "dfs".

Input: startWord = "gedk", targetWord= "geek", wordList = ["geek", "gefk"]


Output: [ [ “gedk”, “geek” ] ]


Explanation: The length of the smallest transformation sequence here is 2.

Following is the only shortest way to get to the targetWord from the startWord :

"gedk" -> ( replace ‘d’ by ‘e’ ) -> "geek".

Input: startWord = "abc", targetWord = "xyz", wordList = ["abc", "ayc", "ayz", "xyz"]

Constraints

  • N= Number of Words
  • M= Length of Word
  • 1 ≤ N ≤ 100
  • 1 ≤ M ≤ 10

Hints

  • "Bidirectional BFS is optimal, reducing the search space by simultaneously expanding from startWord and targetWord until they meet in the middle. Instead of storing only distances, we store parent mappings to track how each word was reached, allowing us to reconstruct all shortest paths efficiently."
  • "After finding the shortest path depth using BFS, we use Depth-First Search (DFS) or backtracking to reconstruct all possible sequences. The parent mapping stores how each word was reached during BFS, and DFS reconstructs sequences by following this mapping back from targetWord to startWord."

Company Tags

MongoDB eBay Instacart McKinsey & Company Oracle Twilio JPMorgan Chase Nutanix American Express Bain & Company Seagate Technology Ubisoft Airbnb Broadcom Robinhood Bloomberg PayPal NVIDIA Salesforce Databricks Cloudflare Zynga Ernst & Young Etsy Mastercard Google Microsoft Amazon Meta Apple Netflix Adobe