File tree 5 files changed +45
-13
lines changed
lcci/01.09.String Rotation
5 files changed +45
-13
lines changed Original file line number Diff line number Diff line change 38
38
39
39
<!-- 这里可写通用的实现逻辑 -->
40
40
41
- 字符串只是旋转的,将前后拼接起来,就能得到一个包含旋转前字符串在内的新字符串。如:
41
+ ** 方法一:字符串匹配 **
42
42
43
- ``` txt
43
+ 首先,如果字符串 $s1$ 和 $s2$ 长度不相等,那么肯定不是旋转字符串。
44
+
45
+ 其次,如果字符串 $s1$ 和 $s2$ 长度相等,那么将两个 $s1$ 连接,得到的 $s1 + s1$ 这个字符串一定包含了 $s1$ 旋转的所有情况,这时候我们只要判断 $s2$ 是否是 $s1 + s1$ 的子串即可。
46
+
47
+ ``` bash
44
48
# 成立
45
49
s1 = " aba"
46
50
s2 = " baa"
47
- s3 = "baabaa" = s2 + s2
48
- ^^^
49
- ```
51
+ s1 + s1 = " abaaba"
52
+ ^^^
50
53
51
- ``` txt
52
54
# 不成立
53
55
s1 = " aba"
54
56
s2 = " bab"
55
- s3 = "babbab" = s2 + s2
57
+ s1 + s1 = " abaaba "
56
58
```
57
59
60
+ 时间复杂度 $O(n)$,空间复杂度 $O(n)$。其中 $n$ 为字符串 $s1$ 的长度。
61
+
58
62
<!-- tabs:start -->
59
63
60
64
### ** Python3**
61
65
62
66
``` python
63
67
class Solution :
64
68
def isFlipedString (self , s1 : str , s2 : str ) -> bool :
65
- return len (s1) == len (s2) and s1 in (s2 * 2 )
69
+ return len (s1) == len (s2) and s2 in s1 * 2
66
70
```
67
71
68
72
### ** Java**
@@ -72,11 +76,22 @@ class Solution:
72
76
``` java
73
77
class Solution {
74
78
public boolean isFlipedString (String s1 , String s2 ) {
75
- return s1. length() == s2. length() && (s2 + s2) . indexOf(s1) != - 1 ;
79
+ return s1. length() == s2. length() && (s1 + s1) . contains(s2) ;
76
80
}
77
81
}
78
82
```
79
83
84
+ ### ** C++**
85
+
86
+ ``` cpp
87
+ class Solution {
88
+ public:
89
+ bool isFlipedString(string s1, string s2) {
90
+ return s1.size() == s2.size() && (s1 + s1).find(s2) != string::npos;
91
+ }
92
+ };
93
+ ```
94
+
80
95
### **Go**
81
96
82
97
```go
Original file line number Diff line number Diff line change 43
43
``` python
44
44
class Solution :
45
45
def isFlipedString (self , s1 : str , s2 : str ) -> bool :
46
- return len (s1) == len (s2) and s1 in (s2 * 2 )
46
+ return len (s1) == len (s2) and s2 in s1 * 2
47
47
```
48
48
49
49
### ** Java**
50
50
51
51
``` java
52
52
class Solution {
53
53
public boolean isFlipedString (String s1 , String s2 ) {
54
- return s1. length() == s2. length() && (s2 + s2) . indexOf(s1) != - 1 ;
54
+ return s1. length() == s2. length() && (s1 + s1) . contains(s2) ;
55
55
}
56
56
}
57
57
```
58
58
59
+ ### ** C++**
60
+
61
+ ``` cpp
62
+ class Solution {
63
+ public:
64
+ bool isFlipedString(string s1, string s2) {
65
+ return s1.size() == s2.size() && (s1 + s1).find(s2) != string::npos;
66
+ }
67
+ };
68
+ ```
69
+
59
70
### **Go**
60
71
61
72
```go
Original file line number Diff line number Diff line change
1
+ class Solution {
2
+ public:
3
+ bool isFlipedString (string s1, string s2) {
4
+ return s1.size () == s2.size () && (s1 + s1).find (s2) != string::npos;
5
+ }
6
+ };
Original file line number Diff line number Diff line change 1
1
class Solution {
2
2
public boolean isFlipedString (String s1 , String s2 ) {
3
- return s1 .length () == s2 .length () && (s2 + s2 ). indexOf ( s1 ) != - 1 ;
3
+ return s1 .length () == s2 .length () && (s1 + s1 ). contains ( s2 ) ;
4
4
}
5
5
}
Original file line number Diff line number Diff line change 1
1
class Solution :
2
2
def isFlipedString (self , s1 : str , s2 : str ) -> bool :
3
- return len (s1 ) == len (s2 ) and s1 in ( s2 * 2 )
3
+ return len (s1 ) == len (s2 ) and s2 in s1 * 2
You can’t perform that action at this time.
0 commit comments