C++Server Side ProgrammingProgramming

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 −

Live Demo

#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;
}

## Input

"hit"
"cog"
["hot","dot","dog","lot","log","cog"]

## Output

5
Updated on 29-Apr-2020 12:23:44