{"id":26611,"date":"2017-12-21T20:37:57","date_gmt":"2017-12-21T15:07:57","guid":{"rendered":"https:\/\/www.wikitechy.com\/technology\/?p=26611"},"modified":"2017-12-21T20:37:57","modified_gmt":"2017-12-21T15:07:57","slug":"c-programming-word-ladder-length-shortest-chain-reach-target-word","status":"publish","type":"post","link":"https:\/\/www.wikitechy.com\/technology\/c-programming-word-ladder-length-shortest-chain-reach-target-word\/","title":{"rendered":"C++ Programming &#8211; Word Ladder Length of shortest chain to reach a target word"},"content":{"rendered":"<p>Given a dictionary, and two words \u2018start\u2019 and \u2018target\u2019 (both of same length). Find length of the smallest chain from \u2018start\u2019 to \u2018target\u2019 if it exists, such that adjacent words in the chain only differ by one character and <span id=\"more-135292\"><\/span>each word in the chain is a valid word i.e., it exists in the dictionary. It may be assumed that the \u2018target\u2019 word exists in dictionary and length of all dictionary words is same.<\/p>\n<p><strong>Example:<\/strong><\/p>\n<pre>Input:  Dictionary = {POON, PLEE, SAME, POIE, PLEA, PLIE, POIN}\r\n             start = TOON\r\n             target = PLEA\r\nOutput: 7\r\nExplanation: TOON - POON - POIN - POIE - PLIE - PLEE - PLEA<\/pre>\n<p>The idea is to use <a href=\"http:\/\/www.geeksforgeeks.org\/breadth-first-traversal-for-a-graph\/\" target=\"_blank\" rel=\"noopener\">BFS<\/a>. We start from the given start word, traverse all words that adjacent (differ by one character) to it and keep doing so until we find the target word or we have traversed all words.<\/p>\n[ad type=&#8221;banner&#8221;]\n<p>Below is C++ implementation of above idea.<\/p>\n<div class=\"code-embed-wrapper\"> <div class=\"code-embed-infos\"> <span class=\"code-embed-name\">C++ Program<\/span> <\/div> <pre class=\"language-cpp code-embed-pre line-numbers\"  data-start=\"1\" data-line-offset=\"0\"><code class=\"language-cpp code-embed-code\">\/\/ C++ program to find length of the shortest chain<br\/>\/\/ transformation from source to target<br\/>#include&lt;bits\/stdc++.h&gt;<br\/>using namespace std;<br\/> <br\/>\/\/ To check if strings differ by exactly one character<br\/>bool isadjacent(string&amp; a, string&amp; b)<br\/>{<br\/>    int count = 0;  \/\/ to store count of differences<br\/>    int n = a.length();<br\/> <br\/>    \/\/ Iterate through all characters and return false<br\/>    \/\/ if there are more than one mismatching characters<br\/>    for (int i = 0; i &lt; n; i++)<br\/>    {<br\/>        if (a[i] != b[i]) count++;<br\/>        if (count &gt; 1) return false;<br\/>    }<br\/>    return count == 1 ? true : false;<br\/>}<br\/> <br\/>\/\/ A queue item to store word and minimum chain length<br\/>\/\/ to reach the word.<br\/>struct QItem<br\/>{<br\/>    string word;<br\/>    int len;<br\/>};<br\/> <br\/>\/\/ Returns length of shortest chain to reach &#039;target&#039; from &#039;start&#039;<br\/>\/\/ using minimum number of adjacent moves.  D is dictionary<br\/>int shortestChainLen(string&amp; start, string&amp; target, set&lt;string&gt; &amp;D)<br\/>{<br\/>    \/\/ Create a queue for BFS and insert &#039;start&#039; as source vertex<br\/>    queue&lt;QItem&gt; Q;<br\/>    QItem item = {start, 1};  \/\/ Chain length for start word is 1<br\/>    Q.push(item);<br\/> <br\/>    \/\/ While queue is not empty<br\/>    while (!Q.empty())<br\/>    {<br\/>        \/\/ Take the front word<br\/>        QItem curr = Q.front();<br\/>        Q.pop();<br\/> <br\/>        \/\/ Go through all words of dictionary<br\/>        for (set&lt;string&gt;::iterator it = D.begin(); it != D.end(); it++)<br\/>        {<br\/>            \/\/ Process a dictionary word if it is adjacent to current<br\/>            \/\/ word (or vertex) of BFS<br\/>            string temp = *it;<br\/>            if (isadjacent(curr.word, temp))<br\/>            {<br\/>                \/\/ Add the dictionary word to Q<br\/>                item.word = temp;<br\/>                item.len = curr.len + 1;<br\/>                Q.push(item);<br\/> <br\/>                \/\/ Remove from dictionary so that this word is not<br\/>                \/\/ processed again.  This is like marking visited<br\/>                D.erase(temp);<br\/> <br\/>                \/\/ If we reached target<br\/>                if (temp == target)<br\/>                  return item.len;<br\/>            }<br\/>        }<br\/>    }<br\/>    return 0;<br\/>}<br\/> <br\/>\/\/ Driver program<br\/>int main()<br\/>{<br\/>    \/\/ make dictionary<br\/>    set&lt;string&gt; D;<br\/>    D.insert(&quot;poon&quot;);<br\/>    D.insert(&quot;plee&quot;);<br\/>    D.insert(&quot;same&quot;);<br\/>    D.insert(&quot;poie&quot;);<br\/>    D.insert(&quot;plie&quot;);<br\/>    D.insert(&quot;poin&quot;);<br\/>    D.insert(&quot;plea&quot;);<br\/>    string start = &quot;toon&quot;;<br\/>    string target = &quot;plea&quot;;<br\/>    cout &lt;&lt; &quot;Length of shortest chain is: &quot;<br\/>         &lt;&lt; shortestChainLen(start, target, D); <br\/>    return 0; <br\/>}<\/code><\/pre> <\/div>\n<p><strong>Output:<\/strong><\/p>\n<pre>Length of shortest chain is: 7<\/pre>\n<p>Time Complexity of the above code is O(n\u00b2m) where n is the number of entries originally in the dictionary and m is the size of the string<\/p>\n[ad type=&#8221;banner&#8221;]\n","protected":false},"excerpt":{"rendered":"<p>Given a dictionary, and two words \u2018start\u2019 and \u2018target\u2019 (both of same length). Find length of the smallest chain from \u2018start\u2019 to \u2018target\u2019 if it exists.<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[69969,83515,1,73906],"tags":[79315,79288,79310,79316,79314,79304,79307,79312,79295,79294,79292,79309,79284,79298,79283,79289,79311,79286,79291,79297,79299,79282,79302,79303,79301,79305,79293,79285,79296,79287,79313,79308,79290,79300,79306],"class_list":["post-26611","post","type-post","status-publish","format-standard","hentry","category-algorithm","category-c-programming-3","category-coding","category-graph-algorithms","tag-another-word-for-ladder","tag-answers-to-word-ladders","tag-easy-word-ladders","tag-how-to-make-a-word-ladder","tag-if-else-ladder-in-java","tag-ladder-2","tag-ladder-code","tag-ladder-dictionary","tag-ladder-problem","tag-ladder-questions","tag-ladder-words","tag-question-ladder","tag-transforma-ladder","tag-what-is-a-word-ladder","tag-word-ladder","tag-word-ladder-2","tag-word-ladder-algorithm","tag-word-ladder-answer-key","tag-word-ladder-answers","tag-word-ladder-answers-free","tag-word-ladder-examples","tag-word-ladder-game","tag-word-ladder-ii","tag-word-ladder-java","tag-word-ladder-leetcode","tag-word-ladder-problem","tag-word-ladder-puzzles","tag-word-ladder-solution","tag-word-ladder-solver","tag-word-ladder-solver-free","tag-word-ladder-with-answers","tag-word-ladders-online","tag-word-ladders-pdf","tag-wordladder","tag-world-ladder"],"_links":{"self":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/posts\/26611","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/comments?post=26611"}],"version-history":[{"count":0,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/posts\/26611\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/media?parent=26611"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/categories?post=26611"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/tags?post=26611"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}