[编程之美]1 字符串移位包含问题

时间复杂度O(n + pm),其中n是s1的长度,p是字符s2[0]在s1出现的次数,m是s2的长度;
空间复杂度O(1)

bool solution(string s1, string s2)
    {
        int j = 0; 
        while (j < s1.length())
        {
            while (j < s1.length() && s1[j] != s2[0])
            {
                j++;
            }
            if (j >= s1.length())
                return false;
            int k = 0, m = j;
            while (k < s2.length() && s1[m] == s2[k])
            {
                k++;
                m = (m + 1) % s1.length();
            }
            if (k == s2.length())
                return true;
            j++;
        }
        return false;
    }
    原文作者:BeLeaderOfMyLife
    原文地址: https://blog.csdn.net/shawjan/article/details/44886807
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞