comments | difficulty | edit_url |
---|---|---|
true |
简单 |
字符串轮转。给定两个字符串s1
和s2
,请编写代码检查s2
是否为s1
旋转而成(比如,waterbottle
是erbottlewat
旋转后的字符串)。
示例1:
输入:s1 = "waterbottle", s2 = "erbottlewat" 输出:True
示例2:
输入:s1 = "aa", "aba" 输出:False
提示:
- 字符串长度在[0, 100000]范围内。
说明:
- 你能只调用一次检查子串的方法吗?
首先,如果字符串
其次,如果字符串
# 成立
s1 = "aba"
s2 = "baa"
s1 + s1 = "abaaba"
^^^
# 不成立
s1 = "aba"
s2 = "bab"
s1 + s1 = "abaaba"
时间复杂度
class Solution:
def isFlipedString(self, s1: str, s2: str) -> bool:
return len(s1) == len(s2) and s2 in s1 * 2
class Solution {
public boolean isFlipedString(String s1, String s2) {
return s1.length() == s2.length() && (s1 + s1).contains(s2);
}
}
class Solution {
public:
bool isFlipedString(string s1, string s2) {
return s1.size() == s2.size() && (s1 + s1).find(s2) != string::npos;
}
};
func isFlipedString(s1 string, s2 string) bool {
return len(s1) == len(s2) && strings.Contains(s1+s1, s2)
}
function isFlipedString(s1: string, s2: string): boolean {
return s1.length === s2.length && (s2 + s2).indexOf(s1) !== -1;
}
impl Solution {
pub fn is_fliped_string(s1: String, s2: String) -> bool {
s1.len() == s2.len() && (s2.clone() + &s2).contains(&s1)
}
}
class Solution {
func isFlippedString(_ s1: String, _ s2: String) -> Bool {
return (s1.isEmpty && s2.isEmpty) || (s1.count == s2.count && (s1 + s1).contains(s2))
}
}