class QueueNode
{
public:
string word;
int length;
QueueNode(const string& str, int l):word(str),length(l){}
};
class Solution {
public:
int ladderLength(string start, string end, unordered_set<string> &dict) {
// Start typing your C/C++ solution below
// DO NOT write int main() function
if(start.empty()||end.empty()||start.size()!=end.size())
return 0;
int nstr = start.size();
unordered_set<string> set;
queue<QueueNode> q;
q.push(QueueNode(start,1));
set.insert(start);
while(!q.empty())
{
QueueNode node = q.front();
q.pop();
if(node.word==end)
return node.length;
for(int i=0;i<nstr;i++)
{
for(int j=0;j<26;j++)
{
string tmp = node.word;
tmp[i] = 'a'+j;
if(dict.count(tmp)==1&&set.count(tmp)==0)
{
set.insert(tmp);
q.push(QueueNode(tmp,node.length+1));
}
}
}
}
return 0;
}
};