Regular Expressions
Solution:
1 用循环brute force 硬找,利用字串长度是source 长度的约数简化运算,O(n^2);
2 复制一遍source头尾相接,去掉首尾两个Character,看剩下的字符串中是否contians source,O(n);
3 利用KMP的最长公共前后缀table,O(n);
4 利用regex// java public boolean repeatedSubstringPattern(String s) { Pattern p = Pattern.compile("(\\w+)\\1+"); Matcher m = p.matcher(s); return m.matches(); } // 或者直接用 return s.matches("regex"); 这样就是java one line。class Solution { public: bool repeatedSubstringPattern(string s) { string t = (s + s).substr(1, s.size() * 2 - 2); return int(t.find(s)) >= 0; } };