diff --git a/Graphs/WordLadderI.cpp b/Graphs/WordLadderI.cpp new file mode 100644 index 0000000..bc7fa49 --- /dev/null +++ b/Graphs/WordLadderI.cpp @@ -0,0 +1,62 @@ +bool isadjacent(string s,string t) +{ + int c=0; + int n=s.size(); + for(int i=0;i1) + return false; + } + + return c==1? true: false; + +} + +struct node +{ + string word; + int len; +}; + +int Solution::ladderLength(string start, string target, vector &dict) { + + if(start==target) + return 1; + + set D; + + for(int i=0;i q; + node item = {start, 1}; + q.push(item); + + while (!q.empty()) + { + node curr = q.front(); + q.pop(); + + for (set::iterator it = D.begin(); it != D.end(); it++) + { + string temp = *it; + if (isadjacent(curr.word, temp)) + { + item.word = temp; + item.len = curr.len + 1; + q.push(item); + + D.erase(temp); + + if (temp == target) + return item.len; + } + } + } + + return 0; +}