mirror of
				https://gitee.com/coder-xiaomo/leetcode-problemset
				synced 2025-11-04 03:33:12 +08:00 
			
		
		
		
	
		
			
				
	
	
		
			13 lines
		
	
	
		
			715 B
		
	
	
	
		
			HTML
		
	
	
	
	
	
			
		
		
	
	
			13 lines
		
	
	
		
			715 B
		
	
	
	
		
			HTML
		
	
	
	
	
	
<p>You have a large text file containing words. Given any two different words, find the shortest distance (in terms of number of words) between them in the file. If the operation will be repeated many times for the same file (but different pairs of words), can you optimize your solution?</p>
 | 
						|
 | 
						|
 | 
						|
 | 
						|
<p><strong>Example: </strong></p>
 | 
						|
 | 
						|
 | 
						|
 | 
						|
<pre>
 | 
						|
 | 
						|
<strong>Input: </strong>words = ["I","am","a","student","from","a","university","in","a","city"], word1 = "a", word2 = "student"
 | 
						|
 | 
						|
<strong>Output: </strong>1</pre>
 | 
						|
 | 
						|
 | 
						|
 | 
						|
<p>Note:</p>
 | 
						|
 | 
						|
 | 
						|
 | 
						|
<ul>
 | 
						|
 | 
						|
	<li><code>words.length <= 100000</code></li>
 | 
						|
 | 
						|
</ul> |