
- C++ Basics
- C++ Home
- C++ Overview
- C++ Environment Setup
- C++ Basic Syntax
- C++ Comments
- C++ Data Types
- C++ Variable Types
- C++ Variable Scope
- C++ Constants/Literals
- C++ Modifier Types
- C++ Storage Classes
- C++ Operators
- C++ Loop Types
- C++ Decision Making
- C++ Functions
- C++ Numbers
- C++ Arrays
- C++ Strings
- C++ Pointers
- C++ References
- C++ Date & Time
- C++ Basic Input/Output
- C++ Data Structures
- C++ Object Oriented
- C++ Classes & Objects
- C++ Inheritance
- C++ Overloading
- C++ Polymorphism
- C++ Abstraction
- C++ Encapsulation
- C++ Interfaces
Word Ladder in C++
Suppose we have two words (beginWord and endWord), and we have dictionary's word list, find the length of shortest transformation sequence from beginWord to endWord, such that −
Only one letter can be converted at a time.
In each transformed word must exist in the word list. The beginWord is not a transformed word.
We have to keep in mind that −
Return 0 when there is no such change sequence.
All words have the same length.
All words contain only lowercase characters.
We can assume no duplicates in the word list.
So if the input is like: beginWord = "hit", endWord = "cog", and wordlist = ["hot", "dot", "dog", "lot", "log", "cog"]
Then the output will be 5, as one shortest transformation is hit → hot → dot → dog → cog
To solve this, we will follow these steps −
Define a method called putStar, this will take j and string s. This will work as follows −
temp := empty string
for i in range 0 to size of s – 1
if i = j, then update temp by concatenating “*” with it, otherwise update temp by concatenating s[i] with temp.
in the main method it will take string b, string e and list of words w, this will work like −
if e is not in w, or b is empty, or e is empty, or w is empty, then return 0
define a map m for string type key and array type value.
for i in range 0 to size of w
x := w[i]
for j := 0 to size of x
inter := putStar(j, x)
insert x into m[inter]
Define a queue q, insert a pair (b, 1) into q
make a map called visited
while q is not empty
s := front pair from q, delete front element from q
x := first element of the pair s, l := second element of the pair s
for i in range 0 to size of x
temp := putStar(i, x)
for j in range 0 to size of m[temp]
aa := m[temp, j]
if aa is same as e, then return l + 1
if visited[aa] is not set, then insert pair (aa, l + 1), and set visited[aa] = 1
level := 0
return 0
Example
Let us see the following implementation to get a better understanding −
#include <bits/stdc++.h> using namespace std; class Solution { public: string putStar(int j, string s){ string temp = ""; for(int i = 0; i < s.size(); i++){ if(i == j)temp += "*"; else temp += s[i]; } return temp; } int ladderLength(string b, string e, vector<string>& w) { if(find(w.begin(), w.end(), e) == w.end() || !b.size() || !e.size() || !w.size())return 0; map < string , vector <string> > m; for(int i = 0; i < w.size(); i++){ string x = w[i]; for(int j = 0; j < x.size(); j++){ string inter = putStar(j,x); m[inter].push_back(x); } } queue < pair <string, int> > q; q.push({b, 1}); map <string, int> visited; while(!q.empty()){ pair < string, int > s = q.front(); q.pop(); string x = s.first; int l = s.second; for(int i = 0; i < x.size(); i++){ string temp = putStar(i ,x); for(int j = 0; j < m[temp].size(); j++){ string aa = m[temp][j]; if(aa == e)return l+1; if(!visited[aa]){ q.push({aa, l+1}); visited[aa] = 1; } } } } int level = 0; return 0; } }; main(){ vector<string> v = {"hot","dot","dog","lot","log","cog"}; Solution ob; cout << (ob.ladderLength("hit", "cog", v)); }
Input
"hit" "cog" ["hot","dot","dog","lot","log","cog"]
Output
5
- Related Articles
- Word Ladder (Length of shortest chain to reach a target word) in C++
- Snake and Ladder Problem
- Explain else-if ladder statement in C language
- Addition multiplication ladder in an array in JavaScript\n
- Java if-else-if ladder statement
- How to calculate HCF using Ladder Division Method?
- A $16.5 ft.$ long ladder is leaned against a wall. The ladder reaches the wall to a height of $13.2 ft.$ Find the distance between the wall and the foot of the ladder.
- A ladder leaning against the wall makes an angle of $60^{o}$ with the horizontal. if the foot of the ladder is $3 m$ away from the wall. find the length of the ladder.
- The angle of elevation of a ladder leaning against a wall is ( 60^{circ} ) and the foot of the ladder is ( 9.5 mathrm{~m} ) away from the wall. Find the length of the ladder.
- A 25 m long ladder is placed against a vertical wall such that the foot of the ladder is 7 m from the feet of wall. If the top of the ladder slides down by 4 m, by how much distance will the foot of the ladder slide?
- ORDERBY word in MySQL?
- Tribonacci Word in C++
- Word Break in Python
- Word Search in Python
- Word Squares in C++
