-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathwordtransform.cpp
125 lines (103 loc) · 3.2 KB
/
wordtransform.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
// {"category": "Algorithm", "notes": "Transform one word into another"}
#include <SDKDDKVer.h>
#include <stdio.h>
#include <tchar.h>
#include <iostream>
#include <map>
#include <queue>
#include <stack>
#include <vector>
#include <Windows.h>
using namespace std;
//------------------------------------------------------------------------------
//
// Given two words of equal length that are in a dictionary, write a method to
// transform one word into another word by changing only one letter at a time.
// The new word you get in each step must be in the dictionary.
//
//------------------------------------------------------------------------------
//------------------------------------------------------------------------------
//
// Implementation
//
//------------------------------------------------------------------------------
void GetOneEditWords(string word, vector<string>& words)
{
for (string::size_type i = 0; i < word.length(); i++)
{
string copy = word;
for (char c = 'a'; c <= 'z'; c++)
{
if (c != word[i])
{
copy[i] = c;
words.push_back(copy);
}
}
}
}
void WordTransform(
string startWord,
string endWord,
map<string, bool>& dictionary)
{
queue<string> wordQueue;
map<string, bool> isVisited;
map<string, string> backtrackMap;
wordQueue.push(startWord);
isVisited[startWord] = true;
while (!wordQueue.empty())
{
string currentWord = wordQueue.front();
wordQueue.pop();
vector<string> nextWords;
GetOneEditWords(currentWord, nextWords);
for (vector<string>::size_type i = 0; i < nextWords.size(); i++)
{
string nextWord = nextWords[i];
if (nextWord == endWord)
{
stack<string> printStack;
printStack.push(endWord);
while (!currentWord.empty())
{
printStack.push(currentWord);
currentWord = backtrackMap[currentWord];
}
while (!printStack.empty())
{
cout << printStack.top().c_str();
printStack.pop();
if (!printStack.empty())
{
cout << " => ";
}
}
cout << endl;
return;
}
if (dictionary[nextWord] && !isVisited[nextWord])
{
wordQueue.push(nextWord);
isVisited[nextWord] = true;
backtrackMap[nextWord] = currentWord;
}
}
}
}
//------------------------------------------------------------------------------
//
// Demo execution
//
//------------------------------------------------------------------------------
int _tmain(int argc, _TCHAR* argv[])
{
const char* c_dictionary[] = { "damp", "lamp", "limp", "lime", "like" };
map<string, bool> dictionary;
for (int i = 0; i < ARRAYSIZE(c_dictionary); i++)
{
dictionary[string(c_dictionary[i])] = true;
}
WordTransform(string("damp"), string("like"), dictionary);
return 0;
}