File tree 8 files changed +145
-2
lines changed
problemset/string-rotation-lcci
8 files changed +145
-2
lines changed Original file line number Diff line number Diff line change 2
2
3
3
<p align =" center " >
4
4
<!-- TOPICS COUNT START -->
5
- <img src =" https://img.shields.io/badge/-进度:507 -green " alt =" 进度:507 " >
5
+ <img src =" https://img.shields.io/badge/-进度:508 -green " alt =" 进度:508 " >
6
6
<!-- TOPICS COUNT END -->
7
7
<a href =" ./assets/docs/TOPICS.md " ><img src =" https://img.shields.io/badge/-题库目录-blue " alt =" 题库目录 " ></a >
8
8
<a href =" ./assets/docs/CATEGORIES.md " ><img src =" https://img.shields.io/badge/-题库分类-red " alt =" 题库分类 " ></a >
Original file line number Diff line number Diff line change 2566
2566
"title" : " 滑动窗口的平均值" ,
2567
2567
"path" : " ./problemset/average-value-of-sliding-window/README.md" ,
2568
2568
"difficulty" : " 简单"
2569
+ },
2570
+ {
2571
+ "id" : " 面试题 01.09" ,
2572
+ "title" : " 字符串轮转" ,
2573
+ "path" : " ./problemset/string-rotation-lcci/README.md" ,
2574
+ "difficulty" : " 简单"
2569
2575
}
2570
2576
]
2571
2577
},
Original file line number Diff line number Diff line change 5068
5068
"difficulty" : " 中等" ,
5069
5069
"url" : " https://leetcode.cn/problems/get-kth-magic-number-lcci/" ,
5070
5070
"path" : " ./problemset/get-kth-magic-number-lcci/README.md"
5071
+ },
5072
+ {
5073
+ "id" : " 面试题 01.09" ,
5074
+ "title" : {
5075
+ "cn" : " 字符串轮转" ,
5076
+ "en" : " string-rotation-lcci"
5077
+ },
5078
+ "difficulty" : " 简单" ,
5079
+ "url" : " https://leetcode.cn/problems/string-rotation-lcci/" ,
5080
+ "path" : " ./problemset/string-rotation-lcci/README.md"
5071
5081
}
5072
5082
]
Original file line number Diff line number Diff line change 469
469
| 面试题 17.11. [ 单词距离] ( ../../problemset/find-closest-lcci/README.md ) | 中等 |
470
470
| 剑指 Offer II 029. [ 排序的循环链表] ( ../../problemset/sorted-circular-linked-list/README.md ) | 中等 |
471
471
| 剑指 Offer II 041. [ 滑动窗口的平均值] ( ../../problemset/average-value-of-sliding-window/README.md ) | 简单 |
472
+ | 面试题 01.09. [ 字符串轮转] ( ../../problemset/string-rotation-lcci/README.md ) | 简单 |
472
473
473
474
## 回溯
474
475
Original file line number Diff line number Diff line change 1012
1012
1013
1013
[ 面试题 01.02. 判定是否互为字符重排] ( ../../problemset/check-permutation-lcci/README.md )
1014
1014
1015
- [ 面试题 17.09. 第 k 个数] ( ../../problemset/get-kth-magic-number-lcci/README.md )
1015
+ [ 面试题 17.09. 第 k 个数] ( ../../problemset/get-kth-magic-number-lcci/README.md )
1016
+
1017
+ [ 面试题 01.09. 字符串轮转] ( ../../problemset/string-rotation-lcci/README.md )
Original file line number Diff line number Diff line change
1
+ # 字符串轮转
2
+
3
+ > 难度:简单
4
+ >
5
+ > https://leetcode.cn/problems/string-rotation-lcci/
6
+
7
+ ## 题目
8
+
9
+ 字符串轮转。给定两个字符串s1和s2,请编写代码检查s2是否为s1旋转而成(比如,waterbottle是erbottlewat旋转后的字符串)。
10
+
11
+ ### 示例
12
+
13
+ #### 示例1:
14
+
15
+ ```
16
+ 输入:s1 = "waterbottle", s2 = "erbottlewat"
17
+ 输出:True
18
+ ```
19
+
20
+ #### 示例2:
21
+
22
+ ```
23
+ 输入:s1 = "aa", s2 = "aba"
24
+ 输出:False
25
+ ```
26
+
27
+ ## 解题
28
+
29
+ ### 模拟
30
+
31
+ ``` ts
32
+ /**
33
+ * 模拟
34
+ * @desc 时间复杂度 O(N²) 空间复杂度 O(1)
35
+ * @param s1
36
+ * @param s2
37
+ * @returns
38
+ */
39
+ export function isFlipedString(s1 : string , s2 : string ): boolean {
40
+ const m = s1 .length
41
+ const n = s2 .length
42
+ if (m !== n ) return false
43
+ if (n === 0 ) return true
44
+
45
+ for (let i = 0 ; i < n ; i ++ ) {
46
+ let flag = true
47
+ for (let j = 0 ; j < n ; j ++ ) {
48
+ if (s1 [(i + j ) % n ] !== s2 [j ]) {
49
+ flag = false
50
+ break
51
+ }
52
+ }
53
+ if (flag ) return true
54
+ }
55
+ return false
56
+ }
57
+ ```
58
+
59
+ ### 搜索子字符串
60
+
61
+ ``` ts
62
+ /**
63
+ * 搜索子字符串
64
+ * @desc 时间复杂度 O(N) 空间复杂度 O(N)
65
+ * @param s1
66
+ * @param s2
67
+ * @returns
68
+ */
69
+ export function isFlipedString2(s1 : string , s2 : string ): boolean {
70
+ return s1 .length === s2 .length && (s1 + s1 ).includes (s2 )
71
+ }
72
+ ```
Original file line number Diff line number Diff line change
1
+ import { describe , expect , it } from 'vitest'
2
+ import { isFlipedString , isFlipedString2 } from '.'
3
+
4
+ describe ( '字符串轮转' , ( ) => {
5
+ describe ( '模拟' , ( ) => testCase ( isFlipedString ) )
6
+ describe ( '搜索子字符串' , ( ) => testCase ( isFlipedString2 ) )
7
+ } )
8
+
9
+ function testCase ( fn : ( s1 : string , s2 : string ) => boolean ) {
10
+ it . each ( [
11
+ [ 'waterbottle' , 'erbottlewat' , true ] ,
12
+ [ 'aa' , 'aba' , false ] ,
13
+ ] ) ( '示例%#' , ( s1 , s2 , expected ) => {
14
+ expect ( fn ( s1 , s2 ) ) . toBe ( expected )
15
+ } )
16
+ }
Original file line number Diff line number Diff line change
1
+ /**
2
+ * 模拟
3
+ * @desc 时间复杂度 O(N²) 空间复杂度 O(1)
4
+ * @param s1
5
+ * @param s2
6
+ * @returns
7
+ */
8
+ export function isFlipedString ( s1 : string , s2 : string ) : boolean {
9
+ const m = s1 . length
10
+ const n = s2 . length
11
+ if ( m !== n ) return false
12
+ if ( n === 0 ) return true
13
+
14
+ for ( let i = 0 ; i < n ; i ++ ) {
15
+ let flag = true
16
+ for ( let j = 0 ; j < n ; j ++ ) {
17
+ if ( s1 [ ( i + j ) % n ] !== s2 [ j ] ) {
18
+ flag = false
19
+ break
20
+ }
21
+ }
22
+ if ( flag ) return true
23
+ }
24
+ return false
25
+ }
26
+
27
+ /**
28
+ * 搜索子字符串
29
+ * @desc 时间复杂度 O(N) 空间复杂度 O(N)
30
+ * @param s1
31
+ * @param s2
32
+ * @returns
33
+ */
34
+ export function isFlipedString2 ( s1 : string , s2 : string ) : boolean {
35
+ return s1 . length === s2 . length && ( s1 + s1 ) . includes ( s2 )
36
+ }
You can’t perform that action at this time.
0 commit comments