Skip to content

Commit d8c1882

Browse files
committed
feat: leetcode 796
1 parent fb1b153 commit d8c1882

File tree

7 files changed

+148
-0
lines changed

7 files changed

+148
-0
lines changed

assets/data/category.json

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1325,6 +1325,12 @@
13251325
"path": "../../problemset/self-dividing-numbers/README.md",
13261326
"difficulty": "简单"
13271327
},
1328+
{
1329+
"id": 796,
1330+
"title": "旋转字符串",
1331+
"path": "../../problemset/rotate-string/README.md",
1332+
"difficulty": "简单"
1333+
},
13281334
{
13291335
"id": 798,
13301336
"title": "得分最高的最小轮调",

assets/data/problems.json

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2249,6 +2249,16 @@
22492249
"url": "https://leetcode-cn.com/problems/prime-number-of-set-bits-in-binary-representation/",
22502250
"path": "../../problemset/prime-number-of-set-bits-in-binary-representation/README.md"
22512251
},
2252+
{
2253+
"id": 796,
2254+
"title": {
2255+
"cn": "旋转字符串",
2256+
"en": "rotate-string"
2257+
},
2258+
"difficulty": "简单",
2259+
"url": "https://leetcode-cn.com/problems/rotate-string/",
2260+
"path": "../../problemset/rotate-string/README.md"
2261+
},
22522262
{
22532263
"id": 798,
22542264
"title": {

assets/docs/CATEGORY.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -262,6 +262,7 @@
262262
| 661. [图片平滑器](../../problemset/image-smoother/README.md) | 简单 |
263263
| 682. [棒球比赛](../../problemset/baseball-game/README.md) | 简单 |
264264
| 728. [自除数](../../problemset/self-dividing-numbers/README.md) | 简单 |
265+
| 796. [旋转字符串](../../problemset/rotate-string/README.md) | 简单 |
265266
| 798. [得分最高的最小轮调](../../problemset/smallest-rotation-with-highest-score/README.md) | 困难 |
266267
| 838. [推多米诺](../../problemset/push-dominoes/README.md) | 中等 |
267268
| 969. [煎饼排序](../../problemset/pancake-sorting/README.md) | 中等 |

assets/docs/PROBLEMS.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -450,6 +450,8 @@
450450

451451
[762. 二进制表示中质数个计算置位](../../problemset/prime-number-of-set-bits-in-binary-representation/README.md)
452452

453+
[796. 旋转字符串](../../problemset/rotate-string/README.md)
454+
453455
[798. 得分最高的最小轮调](../../problemset/smallest-rotation-with-highest-score/README.md)
454456

455457
[838. 推多米诺](../../problemset/push-dominoes/README.md)

problemset/rotate-string/README.md

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
# 旋转字符串
2+
3+
> 难度:简单
4+
>
5+
> https://leetcode-cn.com/problems/rotate-string/
6+
7+
## 题目
8+
9+
给定两个字符串, `s` 和 `goal`。如果在若干次旋转操作之后,`s` 能变成 `goal` ,那么返回 `true` 。
10+
11+
`s` 的 **旋转操作** 就是将 `s `最左边的字符移动到最右边。 
12+
13+
例如, 若 `s = 'abcde'`,在旋转一次之后结果就是`'bcdea'` 。
14+
 
15+
### 示例
16+
17+
#### 示例 1:
18+
19+
```
20+
输入: s = "abcde", goal = "cdeab"
21+
输出: true
22+
```
23+
24+
#### 示例 2:
25+
26+
```
27+
输入: s = "abcde", goal = "abced"
28+
输出: false
29+
```
30+
31+
## 解题
32+
33+
### 模拟
34+
35+
```ts
36+
/**
37+
* 模拟
38+
* @desc 时间复杂度 O(N) 空间复杂度 O(N)
39+
* @param s
40+
* @param goal
41+
* @returns
42+
*/
43+
export function rotateString(s: string, goal: string): boolean {
44+
if (s.length !== goal.length) return false
45+
if (s === goal) return true
46+
47+
const len = s.length
48+
const goalFirst = goal.charAt(0)
49+
for (let i = 0; i < len; i++) {
50+
if (
51+
s.charAt(i + 1) === goalFirst
52+
&& s.substring(i + 1, len) + s.substring(0, i + 1) === goal
53+
) return true
54+
}
55+
56+
return false
57+
}
58+
```
59+
60+
61+
### 搜索子字符串
62+
63+
```ts
64+
/**
65+
* 搜索子字符串
66+
* @desc 时间复杂度 O(N) 空间复杂度 O(N)
67+
* @param s
68+
* @param goal
69+
* @returns
70+
*/
71+
export function rotateString2(s: string, goal: string): boolean {
72+
return s.length === goal.length && (s + s).includes(goal)
73+
}
74+
```
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
import { describe, expect, it } from 'vitest'
2+
import { rotateString, rotateString2 } from '.'
3+
4+
describe('旋转字符串', () => {
5+
describe('模拟', () => {
6+
testCase(rotateString)
7+
})
8+
9+
describe('搜索子字符串', () => {
10+
testCase(rotateString2)
11+
})
12+
})
13+
14+
function testCase(fn: (s: string, goal: string) => boolean) {
15+
it.each([
16+
['abcde', 'cdeab', true],
17+
['abcde', 'abced', false],
18+
['clrwmpkwru', 'wmpkwruclr', true],
19+
])('示例%#', (s, goal, expected) => {
20+
expect(fn(s, goal)).toBe(expected)
21+
})
22+
}

problemset/rotate-string/index.ts

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
/**
2+
* 模拟
3+
* @desc 时间复杂度 O(N) 空间复杂度 O(N)
4+
* @param s
5+
* @param goal
6+
* @returns
7+
*/
8+
export function rotateString(s: string, goal: string): boolean {
9+
if (s.length !== goal.length) return false
10+
if (s === goal) return true
11+
12+
const len = s.length
13+
const goalFirst = goal.charAt(0)
14+
for (let i = 0; i < len; i++) {
15+
if (
16+
s.charAt(i + 1) === goalFirst
17+
&& s.substring(i + 1, len) + s.substring(0, i + 1) === goal
18+
) return true
19+
}
20+
21+
return false
22+
}
23+
24+
/**
25+
* 搜索子字符串
26+
* @desc 时间复杂度 O(N) 空间复杂度 O(N)
27+
* @param s
28+
* @param goal
29+
* @returns
30+
*/
31+
export function rotateString2(s: string, goal: string): boolean {
32+
return s.length === goal.length && (s + s).includes(goal)
33+
}

0 commit comments

Comments
 (0)