clueless coding // TODO: be smarter

LeetCode Repeated Substring Pattern: CPP Solution



TL;DR Code Solution



bool repeatedSubstringHelper(string str, string match) {
    for (int i = 0; i < str.length(); i += match.length()) {
        if (str.substr(i, match.length()) != match) {
            return false;
        }

    }
    return true;
}

bool repeatedSubstringPattern(string str) {
    for (int i = 1; i <= str.length() / 2; i++) {
        if (str.length() % i == 0) {
            if (repeatedSubstringHelper(str, str.substr(0, i))) {
                return true;
            }
        }
    }
    return false;
}