Skip to content

Commit

Permalink
feat: leetcode 6167
Browse files Browse the repository at this point in the history
  • Loading branch information
vancats committed Sep 4, 2022
1 parent 9662965 commit 96748a8
Show file tree
Hide file tree
Showing 7 changed files with 118 additions and 1 deletion.
6 changes: 6 additions & 0 deletions public/data/category.json
Original file line number Diff line number Diff line change
Expand Up @@ -1864,6 +1864,12 @@
"title": "竞赛-从字符串中移除星号",
"path": "../../src/竞赛-从字符串中移除星号/README.md",
"difficulty": "非常简单"
},
{
"id": "6167",
"title": "竞赛-检查相同字母间的距离",
"path": "../../src/竞赛-检查相同字母间的距离/README.md",
"difficulty": "非常简单"
}
]
}
Expand Down
6 changes: 6 additions & 0 deletions public/data/problems.json
Original file line number Diff line number Diff line change
Expand Up @@ -1511,5 +1511,11 @@
"title": "竞赛-给定条件下构造矩阵",
"difficulty": "困难",
"path": "../../src/竞赛-给定条件下构造矩阵/README.md"
},
{
"id": "6167",
"title": "竞赛-检查相同字母间的距离",
"difficulty": "非常简单",
"path": "../../src/竞赛-检查相同字母间的距离/README.md"
}
]
1 change: 1 addition & 0 deletions public/docs/CATEGORY.md
Original file line number Diff line number Diff line change
Expand Up @@ -402,3 +402,4 @@
| [竞赛-矩阵中的局部最大值](../../src/竞赛-矩阵中的局部最大值/README.md) | 简单 |
| [竞赛-边积分最高的节点](../../src/竞赛-边积分最高的节点/README.md) | 非常简单 |
| [竞赛-从字符串中移除星号](../../src/竞赛-从字符串中移除星号/README.md) | 非常简单 |
| [竞赛-检查相同字母间的距离](../../src/竞赛-检查相同字母间的距离/README.md) | 非常简单 |
4 changes: 3 additions & 1 deletion public/docs/PROBLEMS.md
Original file line number Diff line number Diff line change
Expand Up @@ -502,4 +502,6 @@

[竞赛-收集垃圾的最少总时间](../../src/竞赛-收集垃圾的最少总时间/README.md)

[竞赛-给定条件下构造矩阵](../../src/竞赛-给定条件下构造矩阵/README.md)
[竞赛-给定条件下构造矩阵](../../src/竞赛-给定条件下构造矩阵/README.md)

[竞赛-检查相同字母间的距离](../../src/竞赛-检查相同字母间的距离/README.md)
72 changes: 72 additions & 0 deletions src/竞赛-检查相同字母间的距离/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
# 竞赛-检查相同字母间的距离

> 难度:非常简单
## 题目

给你一个下标从 0 开始的字符串 s ,该字符串仅由小写英文字母组成,s 中的每个字母都 恰好 出现 两次 。另给你一个下标从 0 开始、长度为 26 的的整数数组 distance 。

字母表中的每个字母按从 0 到 25 依次编号(即,'a' -> 0, 'b' -> 1, 'c' -> 2, ... , 'z' -> 25)。

在一个 匀整 字符串中,第 i 个字母的两次出现之间的字母数量是 distance[i] 。如果第 i 个字母没有在 s 中出现,那么 distance[i] 可以 忽略 。

如果 s 是一个 匀整 字符串,返回 true ;否则,返回 false 。

### 示例

#### 示例 1:

```
输入:s = "abaccb", distance = [1,3,0,5,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]
输出:true
解释:
- 'a' 在下标 0 和下标 2 处出现,所以满足 distance[0] = 1 。
- 'b' 在下标 1 和下标 5 处出现,所以满足 distance[1] = 3 。
- 'c' 在下标 3 和下标 4 处出现,所以满足 distance[2] = 0 。
注意 distance[3] = 5 ,但是由于 'd' 没有在 s 中出现,可以忽略。
因为 s 是一个匀整字符串,返回 true 。
```

#### 示例 2:

```
输入:s = "aa", distance = [1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]
输出:false
解释:
- 'a' 在下标 0 和 1 处出现,所以两次出现之间的字母数量为 0 。
但是 distance[0] = 1 ,s 不是一个匀整字符串。
```

### 提示:

```
2 <= s.length <= 52
s 仅由小写英文字母组成
s 中的每个字母恰好出现两次
distance.length == 26
0 <= distance[i] <= 50
```

## 题解

```ts
/**
* @description: 时间复杂度 O(N) 空间复杂度 O(1)
* @return {*}
* @param {string} s
* @param {number} distance
*/
export function checkDistances(s: string, distance: number[]): boolean {
for (let i = 0; i < s.length; i++) {
const code = getCharCode(s[i])
if (distance[code] === -1) continue
if (s[i + distance[code] + 1] !== s[i]) return false
distance[code] = -1
}
return true
}

function getCharCode(char: string): number {
return char.charCodeAt(0) - 97
}
```
11 changes: 11 additions & 0 deletions src/竞赛-检查相同字母间的距离/index.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { checkDistances } from '.'

describe('竞赛-检查相同字母间的距离', () => {
it('示例一', () => {
expect(checkDistances('abaccb', [1, 3, 0, 5, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0])).toBe(true)
})

it('示例二', () => {
expect(checkDistances('aa', [1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0])).toBe(false)
})
})
19 changes: 19 additions & 0 deletions src/竞赛-检查相同字母间的距离/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
/**
* @description: 时间复杂度 O(N) 空间复杂度 O(1)
* @return {*}
* @param {string} s
* @param {number} distance
*/
export function checkDistances(s: string, distance: number[]): boolean {
for (let i = 0; i < s.length; i++) {
const code = getCharCode(s[i])
if (distance[code] === -1) continue
if (s[i + distance[code] + 1] !== s[i]) return false
distance[code] = -1
}
return true
}

function getCharCode(char: string): number {
return char.charCodeAt(0) - 97
}

0 comments on commit 96748a8

Please sign in to comment.