Advertisement

Repeated Substring Pattern - LeetCode 459 Solution

Repeated Substring Pattern - Complete Solution Guide

Repeated Substring Pattern is LeetCode problem 459, a Easy level challenge. This complete guide provides step-by-step explanations, multiple solution approaches, and optimized code in python3, java, cpp, c.

Problem Statement

Given a string s , check if it can be constructed by taking a substring of it and appending multiple copies of the substring together. Example 1: Input: s = "abab" Output: true Explanation: It is the substring "ab" twice. Example 2: Input: s = "aba" Output: false Example 3: Input: s = "abcabcabcabc" Output: true Explanation: It is the substring "abc" four times or the substring "abcabc" twice. Constraints: 1 <= s.length <= 10 4 s consists of lowercase English letters.

Detailed Explanation

The problem asks us to determine if a given string `s` can be formed by repeating a substring of itself. In other words, we need to find a substring `sub` such that concatenating `sub` multiple times results in the original string `s`. The input is a string `s` consisting of lowercase English letters, with a length between 1 and 10,000. The output is a boolean value: `true` if `s` can be formed by repeating a substring, and `false` otherwise.

Solution Approach

The solution iterates through all possible lengths of the repeating substring, starting from 1 up to half the length of the original string. For each length, it checks if it's a divisor of the original string's length. If it is, it extracts the substring of that length from the beginning of the string and then constructs a new string by repeating this substring. Finally, it compares the constructed string with the original string. If they are equal, it means the original string can be formed by repeating the substring, so the function returns `true`. If no such substring is found after iterating through all possible lengths, the function returns `false`.

Step-by-Step Algorithm

  1. Step 1: Get the length `n` of the input string `s`.
  2. Step 2: Iterate from `i = 1` to `n // 2` (inclusive). This loop represents the possible lengths of the repeating substring.
  3. Step 3: Inside the loop, check if `n` is divisible by `i` (i.e., `n % i == 0`). If it is, it means `i` could be the length of the repeating substring.
  4. Step 4: If `n % i == 0`, extract the substring `sub` of length `i` from the beginning of `s` (i.e., `s[:i]` in Python, `s.substring(0, i)` in Java/C++).
  5. Step 5: Create a new string `repeated` by repeating the substring `sub` `n // i` times. For example, in python this can be done by `substring * (n // i)`.
  6. Step 6: Compare the `repeated` string with the original string `s`. If they are equal, return `true` because `s` can be formed by repeating `sub`.
  7. Step 7: If the loop finishes without finding any repeating substring, return `false`.

Key Insights

  • Insight 1: If a string `s` is formed by repeating a substring, then the length of the substring must be a divisor of the length of `s`. This is because the substring must fit an integer number of times within `s`.
  • Insight 2: We can iterate through all possible substring lengths that are divisors of the string's length. For each possible length, we extract the substring and check if repeating it generates the original string.
  • Insight 3: The search space for the length of substring can be reduced to be less than or equal to half of the length of original string, since the substring must be repeated at least twice to form the string.

Complexity Analysis

Time Complexity: O(n^2)

Space Complexity: O(n)

Topics

This problem involves: String, String Matching.

Companies

Asked at: Myntra.