Skip to content

459. Repeated Substring Pattern 👍

  • Time: $O(n^2)$
  • Space: $O(n)$
1
2
3
4
5
6
7
class Solution {
 public:
  bool repeatedSubstringPattern(string s) {
    const string ss = s + s;
    return ss.substr(1, ss.length() - 2).find(s) != string::npos;
  }
};
1
2
3
4
5
6
class Solution {
  public boolean repeatedSubstringPattern(String s) {
    final String ss = s + s;
    return ss.substring(1, ss.length() - 1).contains(s);
  }
}
1
2
3
class Solution:
  def repeatedSubstringPattern(self, s: str) -> bool:
    return s in (s + s)[1:-1]