Skip to content

feat: add solutions to lc problem: No.0245 #4433

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 25, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 38 additions & 5 deletions solution/0200-0299/0245.Shortest Word Distance III/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)$。

<!-- tabs:start -->

Expand Down Expand Up @@ -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;
}
```

<!-- tabs:end -->

<!-- solution:end -->
Expand Down
43 changes: 42 additions & 1 deletion solution/0200-0299/0245.Shortest Word Distance III/README_EN.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,14 @@ tags:

<!-- solution:start -->

### 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)$.

<!-- tabs:start -->

Expand Down Expand Up @@ -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;
}
```

<!-- tabs:end -->

<!-- solution:end -->
Expand Down
29 changes: 29 additions & 0 deletions solution/0200-0299/0245.Shortest Word Distance III/Solution.ts
Original file line number Diff line number Diff line change
@@ -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;
}