Leetcode solution 243: Shortest Word Distance

Problem Statement

Given a list of words and two words word1 and word2, return the shortest distance between these two words in the list.

Example:

Assume that words = ["practice", "makes", "perfect", "coding", "makes"].

Input: word1 = “coding”, word2 = “practice”
Output: 3
Input: word1 = "makes", word2 = "coding"
Output: 1

Note:

You may assume that word1 does not equal to word2, and word1 and word2 are both in the list.

Problem link

Video Tutorial

You can find the detailed video tutorial here

Thought Process

It is a bit confusing at first to be confused with the "Edit Distance" problem. However, there are duplicate words in the array, it‘s just a matter of finding the minimum distance between two words, where words could be found in multiple places in the array.

Brute force way (for each word, find the closest distance with the other word) would give you O(N^2) time complexity where N is the array size. An O(N) optimization would be having two indices for each word and keep updating the minimum distance. It is greedy because the closer the two elements are, the smaller the distance would be.

You can refer to Leetcode official solution for a detailed explanation.

Solutions

 1 public int shortestDistance(String[] words, String word1, String word2) {
 2     if (words == null || words.length == 0 || word1 == null || word2 == null || word1.equals(word2)) {
 3         return -1;
 4     }
 5     int minDistance = words.length;
 6
 7     int wordIndex1 = -1;
 8     int wordIndex2 = -1;
 9
10     for (int i = 0; i < words.length; i++) {
11         if (words[i].equals(word1)) {
12             wordIndex1 = i;
13             if (wordIndex2 != -1) {
14                 minDistance = Math.min(minDistance, Math.abs(wordIndex1 - wordIndex2));
15             }
16         }
17         if (words[i].equals(word2)) {
18             wordIndex2 = i;
19             if (wordIndex1 != -1) {
20                 minDistance = Math.min(minDistance, Math.abs(wordIndex1 - wordIndex2));
21             }
22         }
23     }
24
25     return minDistance;
26 }

Implementation

Time Complexity: O(N) where N is the array size

Space Complexity: O(1) Constant space

References

原文地址:https://www.cnblogs.com/baozitraining/p/12100071.html

时间: 2024-10-25 02:03:32

Leetcode solution 243: Shortest Word Distance的相关文章

[LeetCode] 243. Shortest Word Distance 最短单词距离

Given a list of words and two words word1 and word2, return the shortest distance between these two words in the list. For example,Assume that words = ["practice", "makes", "perfect", "coding", "makes"]. G

[LeetCode] 244. Shortest Word Distance II 最短单词距离 II

This is a follow up of Shortest Word Distance. The only difference is now you are given the list of words and your method will be called repeatedly many times with different parameters. How would you optimize it? Design a class which receives a list

245. Shortest Word Distance III 单词可以重复的最短单词距离

[抄题]: Given a list of words and two words word1 and word2, return the shortest distance between these two words in the list. word1 and word2 may be the same and they represent two individual words in the list. Example:Assume that words = ["practice&q

[Swift]LeetCode243.最短单词距离 $ Shortest Word Distance

Given a list of words and two words word1 and word2, return the shortest distance between these two words in the list. For example,Assume that words = ["practice", "makes", "perfect", "coding", "makes"]. G

LeetCode——Length of Last Word

Given a string s consists of upper/lower-case alphabets and empty space characters ' ', return the length of last word in the string. If the last word does not exist, return 0. Note: A word is defined as a character sequence consists of non-space cha

LeetCode: Length of Last Word [057]

昨天同事在做主从时,从库报如下错误: Got fatal error 1236 from master when reading data from binary log: 'Misconfigured master - server id was not set' 粗粗看好像是master的server-id没有设置,但同事做如下查询: 备库采集: [email protected] Fri May 23 14:18:59 2014 14:18:59 [(none)]> show variab

Baozi Leetcode solution 54: Sprial Matrix

Problem Statement  Given a matrix of m x n elements (m rows, n columns), return all elements of the matrix in spiral order. Example 1: Input: [ [ 1, 2, 3 ], [ 4, 5, 6 ], [ 7, 8, 9 ] ] Output: [1,2,3,6,9,8,7,4,5] Example 2: Input: [ [1, 2, 3, 4], [5,

[Solution] 821. Shortest Distance to a Character

Difficulty: Easy Problem Given a string S and a character C, return an array of integers representing the shortest distance from the character C in the string. Example 1: Input: S = "loveleetcode", C = 'e' Output: [3, 2, 1, 0, 1, 0, 0, 1, 2, 2,

Baozi Leetcode solution 72. Edit Distance

Problem Statement Given two words word1 and word2, find the minimum number of operations required to convert word1 to word2. You have the following 3 operations permitted on a word: Insert a character Delete a character Replace a character Example 1: