Question:
Given a dictionary and the two words 'start' and 'target' (both of the same length). The task is to find the length of the shortest chain from 'start' to 'target' where adjacent words in the chain differ by one character, and each word in the chain is a valid word (i.e., it exists in the dictionary).
Assume that the 'target' word is in the dictionary and that all dictionary words are the same length.
Input:
Dictionary: A set of strings of length n.
A ‘start’ string.
A ‘target’ string.
Output:
An integer: Length of the shortest chain from ‘start’ to ‘target.’
The questions you can ask the interviewer:
- What can be the range of the length of the chain?
- What is the range of the length of the words in the dictionary?
Examples:
Alert Ninjas: Don’t stop now! Enroll in the Competitive Programming course today and start coding. Learn the art of writing time and space-efficient code and compete in code competitions worldwide.
Input: Dictionary = {poon, plee, same, poie, pool, plie, poin} start = tool target = poin
Output: 4
Explanation: The smallest chain: tool - pool - poon - poin. ‘t’ in the tool is replaced by ‘p’ to make the word pool. ‘l’ in the pool is replaced by ‘n’ to make the word poon. ‘o’ in the poon is replaced by ‘i’ to make the word poin.
Input: Dictionary = {aaaa, aaab, saaa, aabb, abbb, abcd, saab, abcd} start = caaa target = poin
Output: 6
Explanation: The smallest chain: caaa - aaa - aaab - aabb - abbb - abcb. |
Recommended: Please try to solve it yourself before moving on to the solution.
Solution:
Idea: The idea is to use BFS. Start from the given word and use BFS to reach the target.
Challenge: How to avoid infinite loop or repetition of words?
Solution: Remove the word from the dictionary once it is included in the chain or maintain a set to avoid repetition
Algorithm:
- Declare a queue and add the start word in it.
- Initiate a variable length to count the length of the smallest chain.
- Run a loop until the queue becomes empty or we find our target.
- Traverse all the adjacent words( words that differ by only one character ) to the word on the top of the queue and push them to the queue for BFS.
- Increase the length after traversing all the adjacent words.
- Keep doing it until we find our target word.
- Return the length.
C++
#include <bits/stdc++.h>
// If this word is equal to the target word, return the length. |
Output:
Time Complexity: O(n2m), where n is the length of the string and m is the number of items in the dictionary.
Space Complexity: O(m*n). As a total of m strings of length n are stored in the queue.
FAQs
-
What do you mean by BFS?
Breadth-First Search (BFS) is a vertex-based method for finding the shortest path in a graph. It uses a Queue data structure that follows the first-in, first-out principle. In BFS, one vertex is visited and marked at a time, and then its neighbours are visited and stored in the queue. This is done until the queue becomes empty or you get your result.
-
How do you solve a word ladder?
In a word ladder puzzle, you must replace one letter at a time to make the change happen gradually. Each stage requires you to change one word into another; You cannot change a word into a non-word. Generally, BFS is used to solve these types of puzzles.