Word Ladder Problem Statement
Given two strings, BEGIN
and END
, along with an array of strings DICT
, determine the length of the shortest transformation sequence from BEGIN
to END
. Each transformation involves changing exactly one alphabet, and the resulting word after each transformation must be present in DICT
.
Input:
Integer T representing number of test cases.
For each test case:
A string BEGIN.
A string END.
An integer N representing the length of the DICT array.
N space-separated strings denoting words in DICT.
Output:
For each test case, the output is a single integer indicating the length of the shortest transformation sequence from BEGIN to END. Output each test case result on a separate line.
Example:
# Example 1 Input:
2
hit
cog
5
hot dot dog lot log
hit
hot
2
hot cog
Output:
# Example 1 Output:
5
2
Constraints:
- 1 ≤ T ≤ 5
- 1 ≤ N ≤ 102
- 1 ≤ |S| ≤ 102
- All words have the same length.
- All transformations involve only lowercase English letters.
BEGIN
will not equalEND
.
Note:
Return -1
if no transformation sequence from BEGIN
to END
is possible. Implement the function, as printing is handled separately.

AnswerBot
4mo
The Word Ladder problem involves finding the shortest transformation sequence from one word to another by changing one letter at a time.
Use breadth-first search to find the shortest transformation seq...read more
Help your peers!
Add answer anonymously...
LimeRoad Full Stack Developer interview questions & answers
A Full Stack Developer was asked Q. Valid Parentheses Problem Statement Given a string 'STR' consisting solely of th...read more
A Full Stack Developer was asked Q. Problem: Ninja's Robot The Ninja has a robot which navigates an infinite number ...read more
A Full Stack Developer was asked Q. Group Anagrams Problem Statement Given an array or list of strings called inputS...read more
Popular interview questions of Full Stack Developer
A Full Stack Developer was asked Q1. Valid Parentheses Problem Statement Given a string 'STR' consisting solely of th...read more
A Full Stack Developer was asked Q2. Problem: Ninja's Robot The Ninja has a robot which navigates an infinite number ...read more
A Full Stack Developer was asked Q3. Group Anagrams Problem Statement Given an array or list of strings called inputS...read more
Stay ahead in your career. Get AmbitionBox app


Trusted by over 1.5 Crore job seekers to find their right fit company
80 L+
Reviews
10L+
Interviews
4 Cr+
Salaries
1.5 Cr+
Users
Contribute to help millions
AmbitionBox Awards
Get AmbitionBox app

