← Home

459. Repeated Substring Pattern

LeetCode article · C++ solution
Website made by wuisabel-gif · Original C++ code by keineahnung2345
straightforward implementationC++Markdown
459

Let's make this one less mysterious. For 459. Repeated Substring Pattern, the solution in this repository is mainly a straightforward implementation solution.

Guide

What?

We want to turn the problem statement into a smaller set of decisions the computer can repeat safely. Instead of trying to be clever immediately, read the code as a sequence of questions:

  • What state are we keeping?
  • How do we move from one state to the next?
  • When do we know the answer is already determined?

For this file, the main tools are: straightforward implementation.

Guide

When?

This pattern shows up when the brute force version has too many repeated checks, too many possible branches, or too much bookkeeping to do by hand. The accepted code reduces that pressure by storing exactly the information that remains useful later.

The important function names to track are repeatedSubstringPattern.

Guide

Why?

The point of the implementation is not to make the code longer. It is to avoid doing the same thinking twice.

  • Substring checks are convenient but not free, so they are part of the real complexity story.
  • The final return is not magic; it is the invariant after the loops or recursion have finished doing their accounting.

Guide

How?

Walk through the solution in this order:

  1. Initialize the memory or helper structure.
  2. Process candidates in the order the invariant expects.
  3. Update the answer only when the current state is valid.
  4. Return the value that represents the fully processed input.

The most important competitive-programming habit here is to trust the invariant. Once the invariant is right, the loops become much less scary.

Guide

Complexity

  • Time: O(n) to O(n log n), depending on the dominant loop or data structure operation
  • Space: O(n) in the usual case for auxiliary containers or recursion

Guide

C++ Solution

Your submission

The accepted solution

solution.cpp
01//Runtime: 720 ms, faster than 7.41% of C++ online submissions for Repeated Substring Pattern.
02//Memory Usage: 490.2 MB, less than 10.45% of C++ online submissions for Repeated Substring Pattern.
03
04class Solution {
05public:
06    bool repeatedSubstringPattern(string s) {
07        //l stands for the length of substring
08        //max length of substring cannot exceed s.size()/2
09        for(int l = 1; l <= s.size()/2; l++){
10            int j = 0;
11            // cout << s.substr(0, l) << endl;
12            while(j + l <= s.size() && s.substr(j, l) == s.substr(0, l)){
13                // cout << s.substr(j, l) << endl;
14                j += l;
15            }
16            if(j == s.size()) return true;
17        }
18        return false;
19    }
20};
21
22//https://leetcode.com/problems/repeated-substring-pattern/discuss/94334/Easy-python-solution-with-explaination
23//Runtime: 36 ms, faster than 79.40% of C++ online submissions for Repeated Substring Pattern.
24//Memory Usage: 15.5 MB, less than 62.69% of C++ online submissions for Repeated Substring Pattern.
25
26class Solution {
27public:
28    bool repeatedSubstringPattern(string s) {
29        if(s.size() == 0) return false;
30        string ss = s + s;
31        return ss.substr(1, ss.size()-2).find(s)!=string::npos;
32    }
33};

Cost

Complexity

Time
O(n) to O(n log n), depending on the dominant loop or data structure operation
Dominated by the main traversal, recursion, or data-structure operations in the code.
Space
O(n) in the usual case for auxiliary containers or recursion
Auxiliary state plus the answer structure where the problem requires one.