超巧妙的重复子串解决方法

虽然是一道简单题,但是有一种超巧妙的解决方法

class Solution {
public:
    bool repeatedSubstringPattern(string s) {
        string t = s + s;
        t.erase(t.begin()); t.erase(t.end() - 1); // 掐头去尾
        if (t.find(s) != std::string::npos) return true; // 检测是否还存在那个子串
        return false;
    }
};

 

posted @ 2023-01-16 08:19  heisse  阅读(29)  评论(0编辑  收藏  举报