You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: solution/0200-0299/0245.Shortest Word Distance III/README_EN.md
+42-1Lines changed: 42 additions & 1 deletion
Original file line number
Diff line number
Diff line change
@@ -45,7 +45,14 @@ tags:
45
45
46
46
<!-- solution:start -->
47
47
48
-
### Solution 1
48
+
### Solution 1: Case Analysis
49
+
50
+
First, we check whether $\textit{word1}$ and $\textit{word2}$ are equal:
51
+
52
+
- 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$.
53
+
- 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$.
54
+
55
+
The time complexity is $O(n)$, where $n$ is the length of the array $\textit{wordsDict}$. The space complexity is $O(1)$.
49
56
50
57
<!-- tabs:start -->
51
58
@@ -182,6 +189,40 @@ func abs(x int) int {
182
189
}
183
190
```
184
191
192
+
#### TypeScript
193
+
194
+
```ts
195
+
function shortestWordDistance(wordsDict:string[], word1:string, word2:string):number {
0 commit comments