- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Word Ladder (Length of shortest chain to reach a target word) in C++
In this problem, we are given a dictionary and two words ‘start’ and ‘target’. Our task is to generate a chain (ladder) from start work to target word, the chain is created such that each word differs the other character by only one word and the word should also exist in the dictionary. The target word exists in the dictionary and also the length of all words is the same. The program will return the length of the shortest path from start to target.
Let’s take an example to understand the problem,
Input
Dictionary = {‘HEAL’, ‘HATE’, ‘HEAT’, ‘TEAT’, ‘THAT’, ‘WHAT’ , ‘HAIL’ ‘THAE’} Start = ‘HELL’ Target = ‘THAE’
Output
6
Explanation
HELL - HEAL - HEAT - TEAT - THAT - THAE
To solve this problem, we will do Breadth-first search of the dictionary. Now, step by step find all elements that are one letter away from the previous character. And create a ladder from start to target.
Program to show the implementation of our solution,
Example
#include <bits/stdc++.h> using namespace std; int wordLadder(string start, string target, set<string>& dictionary) { if (dictionary.find(target) == dictionary.end()) return 0; int level = 0, wordlength = start.size(); queue<string> ladder; ladder.push(start); while (!ladder.empty()) { ++level; int sizeOfLadder = ladder.size(); for (int i = 0; i < sizeOfLadder; ++i) { string word = ladder.front(); ladder.pop(); for (int pos = 0; pos < wordlength; ++pos) { char orig_char = word[pos]; for (char c = 'a'; c <= 'z'; ++c) { word[pos] = c; if (word == target) return level + 1; if (dictionary.find(word) == dictionary.end()) continue; dictionary.erase(word); ladder.push(word); } word[pos] = orig_char; } } } return 0; } int main() { set<string> dictionary; dictionary.insert("heal"); dictionary.insert("heat"); dictionary.insert("teat"); dictionary.insert("that"); dictionary.insert("what"); dictionary.insert("thae"); dictionary.insert("hlle"); string start = "hell"; string target = "thae"; cout<<"Length of shortest chain from '"<<start<<"' to '"<<target<<"' is: "<<wordLadder(start, target, dictionary); return 0; }
Output
Length of shortest chain from 'hell' to 'thae' is: 6
- Related Articles
- Word Ladder in C++
- Shortest Word Distance II in C++
- Shortest Word Distance III in C++
- Program to find length of longest diminishing word chain in Python?
- Shortest Completing Word in Python
- Length of Last Word in C++
- Finding shortest word in a string in JavaScript
- Find shortest unique prefix for every word in a given list in C++
- C++ program for length of the longest word in a sentence
- Program to find shortest cycle length holding target in python
- Shortest Distance to Target Color in C++
- C++ program to read file word by word?
- C program to Replace a word in a text by another given word
- Finding average word length of sentences - JavaScript
- How to reverse a given string word by word instead of letters using C#?
