题目来源:
leetcode题目,网址:面试题 01.09. 字符串轮转 - 力扣(LeetCode)
解题思路:
将 s2 与自身拼接,判断 s1 是否为其子串即可。
解题代码:
- class Solution {
- public:
- bool isFlipedString(string s1, string s2) {
- if(s1.length()!=s2.length()){
- return false;
- }
- s2+=s2;
- return s2.find(s1)!=string::npos;
- }
- };
总结:
str1.find(str2) 若结果是 string::npos,则str1 中不包含 str2,否则包含。
官方题解给出了两种解法。一种是模拟,另一种是拼接后搜索子字符串。