<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>word Archives - Huahua&#039;s Tech Road</title>
	<atom:link href="https://zxi.mytechroad.com/blog/tag/word/feed/" rel="self" type="application/rss+xml" />
	<link>https://zxi.mytechroad.com/blog/tag/word/</link>
	<description></description>
	<lastBuildDate>Sun, 24 May 2020 05:15:23 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=6.0.8</generator>

<image>
	<url>https://zxi.mytechroad.com/blog/wp-content/uploads/2017/09/cropped-photo-32x32.jpg</url>
	<title>word Archives - Huahua&#039;s Tech Road</title>
	<link>https://zxi.mytechroad.com/blog/tag/word/</link>
	<width>32</width>
	<height>32</height>
</image> 
	<item>
		<title>花花酱 LeetCode 1455. Check If a Word Occurs As a Prefix of Any Word in a Sentence</title>
		<link>https://zxi.mytechroad.com/blog/string/leetcode-1455-check-if-a-word-occurs-as-a-prefix-of-any-word-in-a-sentence/</link>
					<comments>https://zxi.mytechroad.com/blog/string/leetcode-1455-check-if-a-word-occurs-as-a-prefix-of-any-word-in-a-sentence/#respond</comments>
		
		<dc:creator><![CDATA[zxi]]></dc:creator>
		<pubDate>Sun, 24 May 2020 05:13:48 +0000</pubDate>
				<category><![CDATA[String]]></category>
		<category><![CDATA[easy]]></category>
		<category><![CDATA[prefix]]></category>
		<category><![CDATA[string]]></category>
		<category><![CDATA[word]]></category>
		<guid isPermaLink="false">https://zxi.mytechroad.com/blog/?p=6810</guid>

					<description><![CDATA[<p>Given a&#160;sentence&#160;that consists of some words separated by a&#160;single space, and a&#160;searchWord. You have to check if&#160;searchWord&#160;is a prefix of any word in&#160;sentence. Return&#160;the index&#8230;</p>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/string/leetcode-1455-check-if-a-word-occurs-as-a-prefix-of-any-word-in-a-sentence/">花花酱 LeetCode 1455. Check If a Word Occurs As a Prefix of Any Word in a Sentence</a> appeared first on <a rel="nofollow" href="https://zxi.mytechroad.com/blog">Huahua&#039;s Tech Road</a>.</p>
]]></description>
										<content:encoded><![CDATA[
<p>Given a&nbsp;<code>sentence</code>&nbsp;that consists of some words separated by a&nbsp;<strong>single space</strong>, and a&nbsp;<code>searchWord</code>.</p>



<p>You have to check if&nbsp;<code>searchWord</code>&nbsp;is a prefix of any word in&nbsp;<code>sentence</code>.</p>



<p>Return&nbsp;<em>the index of the word</em>&nbsp;in&nbsp;<code>sentence</code>&nbsp;where&nbsp;<code>searchWord</code>&nbsp;is a prefix of this word (<strong>1-indexed</strong>).</p>



<p>If&nbsp;<code>searchWord</code>&nbsp;is&nbsp;a prefix of more than one word, return the index of the first word&nbsp;<strong>(minimum index)</strong>. If there is no such word return&nbsp;<strong>-1</strong>.</p>



<p>A&nbsp;<strong>prefix</strong>&nbsp;of a string&nbsp;<code>S</code>&nbsp;is any leading contiguous substring of&nbsp;<code>S</code>.</p>



<p><strong>Example 1:</strong></p>



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong> sentence = "i love eating burger", searchWord = "burg"
<strong>Output:</strong> 4
<strong>Explanation:</strong> "burg" is prefix of "burger" which is the 4th word in the sentence.
</pre>



<p><strong>Example 2:</strong></p>



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong> sentence = "this problem is an easy problem", searchWord = "pro"
<strong>Output:</strong> 2
<strong>Explanation:</strong> "pro" is prefix of "problem" which is the 2nd and the 6th word in the sentence, but we return 2 as it's the minimal index.
</pre>



<p><strong>Example 3:</strong></p>



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong> sentence = "i am tired", searchWord = "you"
<strong>Output:</strong> -1
<strong>Explanation:</strong> "you" is not a prefix of any word in the sentence.
</pre>



<p><strong>Example 4:</strong></p>



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong> sentence = "i use triple pillow", searchWord = "pill"
<strong>Output:</strong> 4
</pre>



<p><strong>Example 5:</strong></p>



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong> sentence = "hello from the other side", searchWord = "they"
<strong>Output:</strong> -1
</pre>



<p><strong>Constraints:</strong></p>



<ul><li><code>1 &lt;= sentence.length &lt;= 100</code></li><li><code>1 &lt;= searchWord.length &lt;= 10</code></li><li><code>sentence</code>&nbsp;consists of lowercase English letters and spaces.</li><li><code>searchWord</code>&nbsp;consists of lowercase English letters.</li></ul>



<h2><strong>Solution 1: Brute Force</strong></h2>



<p>Time complexity: O(n)<br>Space complexity: O(1)</p>



<div class="responsive-tabs">
<h2 class="tabtitle">C++</h2>
<div class="tabcontent">

<pre class="crayon-plain-tag">class Solution {
public:
  int isPrefixOfWord(string sentence, string searchWord) {
    const int n = sentence.size();
    const int l = searchWord.size();
    int word = 0;
    for (int i = 0; i &lt;= n - l; ++i) {
      if (i == 0 || sentence[i - 1] == ' ') {
        ++word;
        bool valid = true;
        for (int j = 0; j &lt; l &amp;&amp; valid; ++j)
          valid = valid &amp;&amp; (sentence[i + j] == searchWord[j]);
        if (valid) return word;      
      }
    }
    return -1;
  }
};</pre>
</div></div>



<p></p>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/string/leetcode-1455-check-if-a-word-occurs-as-a-prefix-of-any-word-in-a-sentence/">花花酱 LeetCode 1455. Check If a Word Occurs As a Prefix of Any Word in a Sentence</a> appeared first on <a rel="nofollow" href="https://zxi.mytechroad.com/blog">Huahua&#039;s Tech Road</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://zxi.mytechroad.com/blog/string/leetcode-1455-check-if-a-word-occurs-as-a-prefix-of-any-word-in-a-sentence/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>花花酱 LeetCode 58. Length of Last Word</title>
		<link>https://zxi.mytechroad.com/blog/string/leetcode-58-length-of-last-word/</link>
					<comments>https://zxi.mytechroad.com/blog/string/leetcode-58-length-of-last-word/#respond</comments>
		
		<dc:creator><![CDATA[zxi]]></dc:creator>
		<pubDate>Mon, 30 Sep 2019 15:40:43 +0000</pubDate>
				<category><![CDATA[String]]></category>
		<category><![CDATA[easy]]></category>
		<category><![CDATA[length]]></category>
		<category><![CDATA[string]]></category>
		<category><![CDATA[word]]></category>
		<guid isPermaLink="false">https://zxi.mytechroad.com/blog/?p=5649</guid>

					<description><![CDATA[<p>Given a string&#160;s&#160;consists of upper/lower-case alphabets and empty space characters&#160;' ', return the length of last word in the string. If the last word does&#8230;</p>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/string/leetcode-58-length-of-last-word/">花花酱 LeetCode 58. Length of Last Word</a> appeared first on <a rel="nofollow" href="https://zxi.mytechroad.com/blog">Huahua&#039;s Tech Road</a>.</p>
]]></description>
										<content:encoded><![CDATA[
<p>Given a string&nbsp;<em>s</em>&nbsp;consists of upper/lower-case alphabets and empty space characters&nbsp;<code>' '</code>, return the length of last word in the string.</p>



<p>If the last word does not exist, return 0.</p>



<p><strong>Note:</strong>&nbsp;A word is defined as a character sequence consists of non-space characters only.</p>



<p><strong>Example:</strong></p>



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong> "Hello World"
<strong>Output:</strong> 5</pre>



<h2><strong>Solution:</strong></h2>



<p>There can be tailing spaces, e.g. &#8220;abc   &#8220;, we need to trim the string first and then scan the string in reverse order until a space or reach the beginning of the string.</p>



<p>Time complexity: O(n)<br>Space complexity: O(1)</p>



<div class="responsive-tabs">
<h2 class="tabtitle">C++</h2>
<div class="tabcontent">

<pre class="crayon-plain-tag">// Author: Huahua
class Solution {
public:
  int lengthOfLastWord(string s) {    
    int i = s.length() - 1;
    int l = 0;
    while (i &gt;= 0 &amp;&amp; s[i] == ' ') --i;
    while (i &gt;= 0 &amp;&amp; s[i] != ' ') {
      --i;
      ++l;
    }
    return l;
  }
};</pre>
</div></div>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/string/leetcode-58-length-of-last-word/">花花酱 LeetCode 58. Length of Last Word</a> appeared first on <a rel="nofollow" href="https://zxi.mytechroad.com/blog">Huahua&#039;s Tech Road</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://zxi.mytechroad.com/blog/string/leetcode-58-length-of-last-word/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>花花酱 LeetCode 127. Word Ladder</title>
		<link>https://zxi.mytechroad.com/blog/searching/127-word-ladder/</link>
					<comments>https://zxi.mytechroad.com/blog/searching/127-word-ladder/#comments</comments>
		
		<dc:creator><![CDATA[zxi]]></dc:creator>
		<pubDate>Tue, 26 Sep 2017 05:05:15 +0000</pubDate>
				<category><![CDATA[Search]]></category>
		<category><![CDATA[BFS]]></category>
		<category><![CDATA[medium]]></category>
		<category><![CDATA[shortest path]]></category>
		<category><![CDATA[string]]></category>
		<category><![CDATA[word]]></category>
		<guid isPermaLink="false">http://zxi.mytechroad.com/blog/?p=415</guid>

					<description><![CDATA[<p>https://leetcode.com/problems/word-ladder/description/ Problem: Given two words (beginWord&#160;and&#160;endWord), and a dictionary&#8217;s word list, find the length of shortest transformation sequence from&#160;beginWord&#160;to&#160;endWord, such that: Only one letter can&#8230;</p>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/searching/127-word-ladder/">花花酱 LeetCode 127. Word Ladder</a> appeared first on <a rel="nofollow" href="https://zxi.mytechroad.com/blog">Huahua&#039;s Tech Road</a>.</p>
]]></description>
										<content:encoded><![CDATA[<p><iframe width="500" height="375" src="https://www.youtube.com/embed/vWPCm69MSfs?feature=oembed" frameborder="0" allow="autoplay; encrypted-media" allowfullscreen></iframe></p>
<p><a href="https://leetcode.com/problems/word-ladder/description/">https://leetcode.com/problems/word-ladder/description/</a></p>
<p><strong>Problem:</strong></p>
<p>Given two words (<i>beginWord</i>&nbsp;and&nbsp;<i>endWord</i>), and a dictionary&#8217;s word list, find the length of shortest transformation sequence from&nbsp;<i>beginWord</i>&nbsp;to&nbsp;<i>endWord</i>, such that:</p>
<ol>
<li>Only one letter can be changed at a time.</li>
<li>Each transformed word must exist in the word list. Note that&nbsp;<i>beginWord</i>&nbsp;is&nbsp;<i>not</i>&nbsp;a transformed word.</li>
</ol>
<p>For example,</p>
<p>Given:<br />
<i>beginWord</i>&nbsp;=&nbsp;<code>"hit"</code><br />
<i>endWord</i>&nbsp;=&nbsp;<code>"cog"</code><br />
<i>wordList</i>&nbsp;=&nbsp;<code>["hot","dot","dog","lot","log","cog"]</code></p>
<p>As one shortest transformation is&nbsp;<code>"hit" -&gt; "hot" -&gt; "dot" -&gt; "dog" -&gt; "cog"</code>,<br />
return its length&nbsp;<code>5</code>.</p>
<p><b>Note:</b></p>
<ul>
<li>Return 0 if there is no such transformation sequence.</li>
<li>All words have the same length.</li>
<li>All words contain only lowercase alphabetic characters.</li>
<li>You may assume no duplicates in the word list.</li>
<li>You may assume&nbsp;<i>beginWord</i>&nbsp;and&nbsp;<i>endWord</i>&nbsp;are non-empty and are not the same.</li>
</ul>
<p>&nbsp;</p>
<p><strong>Idea:</strong></p>
<p><script async="" src="https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script><br />
<ins class="adsbygoogle" style="display:block" data-ad-format="fluid" data-ad-layout-key="-fb+5w+4e-db+86" data-ad-client="ca-pub-2404451723245401" data-ad-slot="2162692788"></ins><br />
<script><br />
     (adsbygoogle = window.adsbygoogle || []).push({});<br />
</script></p>
<p>BFS</p>
<p><span style="font-weight: 400;">Time Complexity: O(n*26^</span><span style="font-weight: 400;">l</span><span style="font-weight: 400;">) -&gt; O(n*26^</span><span style="font-weight: 400;">l/2</span><span style="font-weight: 400;">), l = len(word), n=|wordList|</span></p>
<p><span style="font-weight: 400;">Space Complexity: O(n)</span></p>
<p><a href="http://zxi.mytechroad.com/blog/wp-content/uploads/2017/09/127-ep71-1.png"><img class="alignnone size-full wp-image-423" src="http://zxi.mytechroad.com/blog/wp-content/uploads/2017/09/127-ep71-1.png" alt="" width="960" height="540" srcset="https://zxi.mytechroad.com/blog/wp-content/uploads/2017/09/127-ep71-1.png 960w, https://zxi.mytechroad.com/blog/wp-content/uploads/2017/09/127-ep71-1-300x169.png 300w, https://zxi.mytechroad.com/blog/wp-content/uploads/2017/09/127-ep71-1-768x432.png 768w, https://zxi.mytechroad.com/blog/wp-content/uploads/2017/09/127-ep71-1-624x351.png 624w" sizes="(max-width: 960px) 100vw, 960px" /></a></p>
<p><a href="http://zxi.mytechroad.com/blog/wp-content/uploads/2017/09/127-ep71-2.png"><img class="alignnone size-full wp-image-422" src="http://zxi.mytechroad.com/blog/wp-content/uploads/2017/09/127-ep71-2.png" alt="" width="960" height="540" srcset="https://zxi.mytechroad.com/blog/wp-content/uploads/2017/09/127-ep71-2.png 960w, https://zxi.mytechroad.com/blog/wp-content/uploads/2017/09/127-ep71-2-300x169.png 300w, https://zxi.mytechroad.com/blog/wp-content/uploads/2017/09/127-ep71-2-768x432.png 768w, https://zxi.mytechroad.com/blog/wp-content/uploads/2017/09/127-ep71-2-624x351.png 624w" sizes="(max-width: 960px) 100vw, 960px" /></a></p>
<p><a href="http://zxi.mytechroad.com/blog/wp-content/uploads/2017/09/127-ep71-4.png"><img class="alignnone size-full wp-image-420" src="http://zxi.mytechroad.com/blog/wp-content/uploads/2017/09/127-ep71-4.png" alt="" width="960" height="540" srcset="https://zxi.mytechroad.com/blog/wp-content/uploads/2017/09/127-ep71-4.png 960w, https://zxi.mytechroad.com/blog/wp-content/uploads/2017/09/127-ep71-4-300x169.png 300w, https://zxi.mytechroad.com/blog/wp-content/uploads/2017/09/127-ep71-4-768x432.png 768w, https://zxi.mytechroad.com/blog/wp-content/uploads/2017/09/127-ep71-4-624x351.png 624w" sizes="(max-width: 960px) 100vw, 960px" /></a></p>
<p><a href="http://zxi.mytechroad.com/blog/wp-content/uploads/2017/09/127-ep71-3.png"><img class="alignnone size-full wp-image-421" src="http://zxi.mytechroad.com/blog/wp-content/uploads/2017/09/127-ep71-3.png" alt="" width="960" height="540" srcset="https://zxi.mytechroad.com/blog/wp-content/uploads/2017/09/127-ep71-3.png 960w, https://zxi.mytechroad.com/blog/wp-content/uploads/2017/09/127-ep71-3-300x169.png 300w, https://zxi.mytechroad.com/blog/wp-content/uploads/2017/09/127-ep71-3-768x432.png 768w, https://zxi.mytechroad.com/blog/wp-content/uploads/2017/09/127-ep71-3-624x351.png 624w" sizes="(max-width: 960px) 100vw, 960px" /></a></p>
<p><script async="" src="//pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script><br />
<ins class="adsbygoogle" style="display: block; text-align: center;" data-ad-layout="in-article" data-ad-format="fluid" data-ad-client="ca-pub-2404451723245401" data-ad-slot="7983117522"></ins><br />
<script><br />
     (adsbygoogle = window.adsbygoogle || []).push({});<br />
</script></p>
<h1><strong>Solution 1: BFS</strong></h1>
<p><div class="responsive-tabs">
<h2 class="tabtitle">C++</h2>
<div class="tabcontent">
</p><pre class="crayon-plain-tag">// Author: Huahua
// Running time: 93 ms
class Solution {
public:
    int ladderLength(string beginWord, string endWord, vector&lt;string&gt;&amp; wordList) {
        unordered_set&lt;string&gt; dict(wordList.begin(), wordList.end());        
        if (!dict.count(endWord)) return 0;
        
        queue&lt;string&gt; q;
        q.push(beginWord);
        
        int l = beginWord.length();
        int step = 0;
        
        while (!q.empty()) {
            ++step;
            for (int size = q.size(); size &gt; 0; size--) {                
                string w = q.front();                
                q.pop();
                for (int i = 0; i &lt; l; i++) {                
                    char ch = w[i];
                    for (int j = 'a'; j &lt;= 'z'; j++) {
                        w[i] = j;
                        // Found the solution
                        if (w == endWord) return step + 1;
                        // Not in dict, skip it
                        if (!dict.count(w)) continue;
                        // Remove new word from dict
                        dict.erase(w);
                        // Add new word into queue
                        q.push(w);                    
                    }
                    w[i] = ch;
                }
            }
        }
        return 0;
    }
};</pre><p></div><h2 class="tabtitle">Java</h2>
<div class="tabcontent">
</p><pre class="crayon-plain-tag">// Author: Huahua
// Running time: 92 ms (&lt;76.61%)
class Solution {
  public int ladderLength(String beginWord, String endWord, List&lt;String&gt; wordList) {
    Set&lt;String&gt; dict = new HashSet&lt;&gt;();
    for (String word : wordList) dict.add(word);
    
    if (!dict.contains(endWord)) return 0;
    
    Queue&lt;String&gt; q = new ArrayDeque&lt;&gt;();
    q.offer(beginWord);
    
    int l = beginWord.length();
    int steps = 0;
    
    while (!q.isEmpty()) {
      ++steps;
      for (int s = q.size(); s &gt; 0; --s) {
        String w = q.poll();        
        char[] chs = w.toCharArray();
        for (int i = 0; i &lt; l; ++i) {
          char ch = chs[i];
          for (char c = 'a'; c &lt;= 'z'; ++c) {
            if (c == ch) continue;
            chs[i] = c;
            String t = new String(chs);         
            if (t.equals(endWord)) return steps + 1;            
            if (!dict.contains(t)) continue;            
            dict.remove(t);            
            q.offer(t);
          }
          chs[i] = ch;
        }
      }
    }
    return 0;
  }
}</pre><p></div></div></p>
<h1><strong>Solution 2:&nbsp;Bidirectional BFS</strong></h1>
<p><div class="responsive-tabs">
<h2 class="tabtitle">C++</h2>
<div class="tabcontent">
</p><pre class="crayon-plain-tag">// Author: Huahua
// Running time: 32 ms (better than 96.6%)
class Solution {
public:
    int ladderLength(string beginWord, string endWord, vector&lt;string&gt;&amp; wordList) {
        unordered_set&lt;string&gt; dict(wordList.begin(), wordList.end());        
        if (!dict.count(endWord)) return 0;
        
        int l = beginWord.length();
        
        unordered_set&lt;string&gt; q1{beginWord};
        unordered_set&lt;string&gt; q2{endWord};
                
        int step = 0;
        
        while (!q1.empty() &amp;&amp; !q2.empty()) {
            ++step;
            
            // Always expend the smaller queue first
            if (q1.size() &gt; q2.size())
                std::swap(q1, q2);
                        
            unordered_set&lt;string&gt; q;
            
            for (string w : q1) {
                for (int i = 0; i &lt; l; i++) {
                    char ch = w[i];
                    for (int j = 'a'; j &lt;= 'z'; j++) {
                        w[i] = j;
                        if (q2.count(w)) return step + 1;
                        if (!dict.count(w)) continue;                        
                        dict.erase(w);
                        q.insert(w);
                    }
                    w[i] = ch;
                }
            }
            
            std::swap(q, q1);
        }
        
        return 0;
    }
};</pre><p></div><h2 class="tabtitle">Java</h2>
<div class="tabcontent">
</p><pre class="crayon-plain-tag">// Author: Huahua
// Running time: 31 ms (&lt;95.17%)
class Solution {
  public int ladderLength(String beginWord, String endWord, List&lt;String&gt; wordList) {
    Set&lt;String&gt; dict = new HashSet&lt;&gt;();
    for (String word : wordList) dict.add(word);
    
    if (!dict.contains(endWord)) return 0;
    
    Set&lt;String&gt; q1 = new HashSet&lt;&gt;();
    Set&lt;String&gt; q2 = new HashSet&lt;&gt;();
    q1.add(beginWord);
    q2.add(endWord);
    
    int l = beginWord.length();
    int steps = 0;
    
    while (!q1.isEmpty() &amp;&amp; !q2.isEmpty()) {
      ++steps;
      
      if (q1.size() &gt; q2.size()) {
        Set&lt;String&gt; tmp = q1;
        q1 = q2;
        q2 = tmp;
      }
      
      Set&lt;String&gt; q = new HashSet&lt;&gt;();
      
      for (String w : q1) {        
        char[] chs = w.toCharArray();
        for (int i = 0; i &lt; l; ++i) {
          char ch = chs[i];
          for (char c = 'a'; c &lt;= 'z'; ++c) {
            chs[i] = c;
            String t = new String(chs);         
            if (q2.contains(t)) return steps + 1;            
            if (!dict.contains(t)) continue;            
            dict.remove(t);        
            q.add(t);
          }
          chs[i] = ch;
        }
      }
      
      q1 = q;
    }
    return 0;
  }
}</pre><p></div><h2 class="tabtitle">Python</h2>
<div class="tabcontent">
</p><pre class="crayon-plain-tag">"""
Author: Huahua
Running time: 115 ms (better than 96.84%)
"""
class Solution(object):
    def ladderLength(self, beginWord, endWord, wordList):
        wordDict = set(wordList)
        if endWord not in wordDict: return 0
        
        l = len(beginWord)
        s1 = {beginWord}
        s2 = {endWord}
        wordDict.remove(endWord)
        step = 0
        while len(s1) &gt; 0 and len(s2) &gt; 0:
            step += 1
            if len(s1) &gt; len(s2): s1, s2 = s2, s1
            s = set()   
            for w in s1:
                new_words = [
                    w[:i] + t + w[i+1:]  for t in string.ascii_lowercase for i in xrange(l)]
                for new_word in new_words:
                    if new_word in s2: return step + 1
                    if new_word not in wordDict: continue
                    wordDict.remove(new_word)                        
                    s.add(new_word)
            s1 = s
        
        return 0</pre><p></div></div></p>
<h1><strong>Related Problems</strong></h1>
<ul>
<li><a href="http://zxi.mytechroad.com/blog/searching/leetcode-126-word-ladder-ii/">[解题报告] LeetCode 126. Word Ladder II</a></li>
<li><a href="http://zxi.mytechroad.com/blog/leetcode/leetcode-79-word-search/">[解题报告] LeetCode 79. Word Search</a></li>
<li><a href="http://zxi.mytechroad.com/blog/leetcode/leetcode-139-word-break/">[解题报告] Leetcode 139. Word Break 花花酱</a></li>
<li><a href="http://zxi.mytechroad.com/blog/leetcode/leetcode-140-word-break-ii/">花花酱 Leetcode 140. Word Break II</a></li>
</ul>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/searching/127-word-ladder/">花花酱 LeetCode 127. Word Ladder</a> appeared first on <a rel="nofollow" href="https://zxi.mytechroad.com/blog">Huahua&#039;s Tech Road</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://zxi.mytechroad.com/blog/searching/127-word-ladder/feed/</wfw:commentRss>
			<slash:comments>3</slash:comments>
		
		
			</item>
	</channel>
</rss>
