Leetcode's question about word distance

It doesn't matter if you try a few more times, whether it's love or reasoning, today or tomorrow.

Algorithm Conan

1. Word distance problem

243. Minimum Word Distance

245. Shortest Word Distance III


1 Description of the topic

Given an array of strings wordDict and two strings word1 and word2 already present in the array. Returns the shortest distance between these two words in the list.

Example 1:

输入: wordsDict = [“practice”, “makes”, “perfect”, “coding”, “makes”], word1 = “coding”, word2 = “practice”

output: 3

Example 2:

输入: wordsDict = [“practice”, “makes”, “perfect”, “coding”, “makes”], word1 = “makes”, word2 = “coding”

output: 1

2 Problem-solving ideas

abstract model

During a traversal, we obtain the subscripts of the two target words, and then calculate their minimum distance.

There are two situations for this question for word1 and word2:

  1. word1 != word2;
  2. Word1 and word2 may be the same;

3. Code implementation

3.1 For the case of word1 != word2

public int shortestDistance(String[] wordsDict, String word1, String word2) {
    
    
        if(wordsDict == null || wordsDict.length == 0) return 0;
        int i1 = -1;
        int i2 = -1;

        int minDistance = wordsDict.length;

        for(int i = 0; i < wordsDict.length; i++){
    
    
            if(word1.equals(wordsDict[i])){
    
    
                i1 = i;

                if(i2 != -1){
    
    
                    minDistance = Math.min(minDistance,i1-i2);
                }
            }

            if(word2.equals(wordsDict[i])){
    
    
                i2 = i;

                if(i1 != -1){
    
    
                    minDistance = Math.min(minDistance,i2-i1);
                }
            }
        }
        return minDistance;
    }

3.2 For the case where word1 may be equal to word2

We only need to filter out the case where two indexes point to the same word (otherwise the distance will be calculated as 0)

public int shortestDistance(String[] wordsDict, String word1, String word2) {
    
    
        if(wordsDict == null || wordsDict.length == 0) return 0;
        int i1 = -1;
        int i2 = -1;

        int minDistance = wordsDict.length;

        for(int i = 0; i < wordsDict.length; i++){
    
    
            if(word1.equals(wordsDict[i])){
    
    
                i1 = i;

                if(i2 != -1){
    
    
                    minDistance = Math.min(minDistance,i1-i2);
                }
            }

            if(word2.equals(wordsDict[i])){
    
    
                i2 = i;
                // 注意这里的判断:i1 != i2
                if(i1 != -1 && i1 != i2){
    
    
                    minDistance = Math.min(minDistance,i2-i1);
                }
            }
        }
        return minDistance;
    }

PS: The equals() method is used for string comparison in Java.

eggs


Welcome to subscribe to the official account and keep asking questions together.

Bertking

Guess you like

Origin blog.csdn.net/wangcheeng/article/details/124262646