diff --git a/solution/0200-0299/0245.Shortest Word Distance III/README.md b/solution/0200-0299/0245.Shortest Word Distance III/README.md index 51d9475fc32ee..379385980b016 100644 --- a/solution/0200-0299/0245.Shortest Word Distance III/README.md +++ b/solution/0200-0299/0245.Shortest Word Distance III/README.md @@ -56,13 +56,12 @@ tags: ### 方法一:分情况讨论 -先判断 `word1` 和 `word2` 是否相等: +我们首先判断 $\textit{word1}$ 和 $\textit{word2}$ 是否相等: -如果相等,遍历数组 `wordsDict`,找到两个 `word1` 的下标 $i$ 和 $j$,求 $i-j$ 的最小值。 +- 如果相等,遍历数组 $\textit{wordsDict}$,找到两个 $\textit{word1}$ 的下标 $i$ 和 $j$,求 $i-j$ 的最小值。 +- 如果不相等,遍历数组 $\textit{wordsDict}$,找到 $\textit{word1}$ 和 $\textit{word2}$ 的下标 $i$ 和 $j$,求 $i-j$ 的最小值。 -如果不相等,遍历数组 `wordsDict`,找到 `word1` 和 `word2` 的下标 $i$ 和 $j$,求 $i-j$ 的最小值。 - -时间复杂度 $O(n)$,空间复杂度 $O(1)$。其中 $n$ 为数组 `wordsDict` 的长度。 +时间复杂度 $O(n)$,其中 $n$ 为数组 $\textit{wordsDict}$ 的长度。空间复杂度 $O(1)$。 @@ -199,6 +198,40 @@ func abs(x int) int { } ``` +#### TypeScript + +```ts +function shortestWordDistance(wordsDict: string[], word1: string, word2: string): number { + let ans = wordsDict.length; + if (word1 === word2) { + let j = -1; + for (let i = 0; i < wordsDict.length; i++) { + if (wordsDict[i] === word1) { + if (j !== -1) { + ans = Math.min(ans, i - j); + } + j = i; + } + } + } else { + let i = -1, + j = -1; + for (let k = 0; k < wordsDict.length; k++) { + if (wordsDict[k] === word1) { + i = k; + } + if (wordsDict[k] === word2) { + j = k; + } + if (i !== -1 && j !== -1) { + ans = Math.min(ans, Math.abs(i - j)); + } + } + } + return ans; +} +``` + diff --git a/solution/0200-0299/0245.Shortest Word Distance III/README_EN.md b/solution/0200-0299/0245.Shortest Word Distance III/README_EN.md index 72ab891b39ce6..6fbcb289984fb 100644 --- a/solution/0200-0299/0245.Shortest Word Distance III/README_EN.md +++ b/solution/0200-0299/0245.Shortest Word Distance III/README_EN.md @@ -45,7 +45,14 @@ tags: -### Solution 1 +### Solution 1: Case Analysis + +First, we check whether $\textit{word1}$ and $\textit{word2}$ are equal: + +- If they are equal, iterate through the array $\textit{wordsDict}$ to find two indices $i$ and $j$ of $\textit{word1}$, and compute the minimum value of $i-j$. +- If they are not equal, iterate through the array $\textit{wordsDict}$ to find the indices $i$ of $\textit{word1}$ and $j$ of $\textit{word2}$, and compute the minimum value of $i-j$. + +The time complexity is $O(n)$, where $n$ is the length of the array $\textit{wordsDict}$. The space complexity is $O(1)$. @@ -182,6 +189,40 @@ func abs(x int) int { } ``` +#### TypeScript + +```ts +function shortestWordDistance(wordsDict: string[], word1: string, word2: string): number { + let ans = wordsDict.length; + if (word1 === word2) { + let j = -1; + for (let i = 0; i < wordsDict.length; i++) { + if (wordsDict[i] === word1) { + if (j !== -1) { + ans = Math.min(ans, i - j); + } + j = i; + } + } + } else { + let i = -1, + j = -1; + for (let k = 0; k < wordsDict.length; k++) { + if (wordsDict[k] === word1) { + i = k; + } + if (wordsDict[k] === word2) { + j = k; + } + if (i !== -1 && j !== -1) { + ans = Math.min(ans, Math.abs(i - j)); + } + } + } + return ans; +} +``` + diff --git a/solution/0200-0299/0245.Shortest Word Distance III/Solution.ts b/solution/0200-0299/0245.Shortest Word Distance III/Solution.ts new file mode 100644 index 0000000000000..acb406b3b5ec7 --- /dev/null +++ b/solution/0200-0299/0245.Shortest Word Distance III/Solution.ts @@ -0,0 +1,29 @@ +function shortestWordDistance(wordsDict: string[], word1: string, word2: string): number { + let ans = wordsDict.length; + if (word1 === word2) { + let j = -1; + for (let i = 0; i < wordsDict.length; i++) { + if (wordsDict[i] === word1) { + if (j !== -1) { + ans = Math.min(ans, i - j); + } + j = i; + } + } + } else { + let i = -1, + j = -1; + for (let k = 0; k < wordsDict.length; k++) { + if (wordsDict[k] === word1) { + i = k; + } + if (wordsDict[k] === word2) { + j = k; + } + if (i !== -1 && j !== -1) { + ans = Math.min(ans, Math.abs(i - j)); + } + } + } + return ans; +}