File tree Expand file tree Collapse file tree 7 files changed +148
-0
lines changed Expand file tree Collapse file tree 7 files changed +148
-0
lines changed Original file line number Diff line number Diff line change 1325
1325
"path" : " ../../problemset/self-dividing-numbers/README.md" ,
1326
1326
"difficulty" : " 简单"
1327
1327
},
1328
+ {
1329
+ "id" : 796 ,
1330
+ "title" : " 旋转字符串" ,
1331
+ "path" : " ../../problemset/rotate-string/README.md" ,
1332
+ "difficulty" : " 简单"
1333
+ },
1328
1334
{
1329
1335
"id" : 798 ,
1330
1336
"title" : " 得分最高的最小轮调" ,
Original file line number Diff line number Diff line change 2249
2249
"url" : " https://leetcode-cn.com/problems/prime-number-of-set-bits-in-binary-representation/" ,
2250
2250
"path" : " ../../problemset/prime-number-of-set-bits-in-binary-representation/README.md"
2251
2251
},
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
+ },
2252
2262
{
2253
2263
"id" : 798 ,
2254
2264
"title" : {
Original file line number Diff line number Diff line change 262
262
| 661. [ 图片平滑器] ( ../../problemset/image-smoother/README.md ) | 简单 |
263
263
| 682. [ 棒球比赛] ( ../../problemset/baseball-game/README.md ) | 简单 |
264
264
| 728. [ 自除数] ( ../../problemset/self-dividing-numbers/README.md ) | 简单 |
265
+ | 796. [ 旋转字符串] ( ../../problemset/rotate-string/README.md ) | 简单 |
265
266
| 798. [ 得分最高的最小轮调] ( ../../problemset/smallest-rotation-with-highest-score/README.md ) | 困难 |
266
267
| 838. [ 推多米诺] ( ../../problemset/push-dominoes/README.md ) | 中等 |
267
268
| 969. [ 煎饼排序] ( ../../problemset/pancake-sorting/README.md ) | 中等 |
Original file line number Diff line number Diff line change 450
450
451
451
[ 762. 二进制表示中质数个计算置位] ( ../../problemset/prime-number-of-set-bits-in-binary-representation/README.md )
452
452
453
+ [ 796. 旋转字符串] ( ../../problemset/rotate-string/README.md )
454
+
453
455
[ 798. 得分最高的最小轮调] ( ../../problemset/smallest-rotation-with-highest-score/README.md )
454
456
455
457
[ 838. 推多米诺] ( ../../problemset/push-dominoes/README.md )
Original file line number Diff line number Diff line change
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
+ ```
Original file line number Diff line number Diff line change
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
+ }
Original file line number Diff line number Diff line change
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
+ }
You can’t perform that action at this time.
0 commit comments