




Sponsored
Sponsored
One approach is to take a substring of the given string and repeatedly concatenate to check if it forms the original string. This involves iterating through possible substring lengths and using modular arithmetic to assess potential repeats.
Time Complexity: O(n^2). Space Complexity: O(n).
1var repeatedSubstringPattern = function(s) {
2    let n = s.length;
3    for (let i = 1; i <= n / 2; i++) {
4        if (n % i === 0) {
5            let part = s.substring(0, i);
6            let repeated = part.repeat(n / i);
7            if (repeated === s) {
8                return true;
9            }
10        }
11    }
12    return false;
13};JavaScript's solution uses repeat function to form string patterns dynamically for comparison.
Another approach is using the property of doubled strings. By creating a new string by repeating the original and removing the first and last character, we can check if the original string exists within this new string.
Time Complexity: O(n). Space Complexity: O(n).
1#include<string>
using namespace std;
class Solution {
public:
    bool repeatedSubstringPattern(string s) {
        string ss = s + s;
        return ss.substr(1, ss.size() - 2).find(s) != string::npos;
    }
};C++ solution utilizes substring and find standard library functions on the concatenated string derived from repeating the string, checking existence.