LeetCode Solutions

459. Repeated Substring Pattern

Time: $O(n^2)$, where $n = |\texttt{s}|$

Space: $O(n)$

			

class Solution {
 public:
  bool repeatedSubstringPattern(string s) {
    const string ss = s + s;
    return ss.substr(1, ss.length() - 2).find(s) != string::npos;
  }
};
			

class Solution {
  public boolean repeatedSubstringPattern(String s) {
    final String ss = s + s;
    return ss.substring(1, ss.length() - 1).contains(s);
  }
}
			

class Solution:
  def repeatedSubstringPattern(self, s: str) -> bool:
    return s in (s + s)[1:-1]