Repeated Substring Pattern using Python

The problem of the Repeated Substring Pattern is a popular coding interview question. Here you will be given a string, and you need to check if the given string can be formed by repeating a substring of the string. So, if you want to know how to solve this problem, this article is for you. This article will take you through how to solve the Repeated Substring Pattern problem using Python.

Repeated Substring Pattern Problem

In the Repeated Substring Pattern problem, you will be given a string. To solve this problem, you need to check if the given string can be formed by repeating a substring of the string. For example, look at the input and output of this problem shown below:

  • Input: “abab” | Output: True

In this example, there’s a string given as an input “abab”. And when a substring (“ab”) of the string (“abab”) is repeated twice, it forms the string as given in the input. So the output is true!

Repeated Substring Pattern using Python

I hope you have now understood what the problem of the Repeated Substring Pattern means. Below is how you can solve this problem using the Python programming language:

def repeatedSubstringPattern(s):
    string = (s + s)[1:-1]
    return string.find(s) != -1

print(repeatedSubstringPattern("abcabcabcabc"))
Output: True

So this is how you can solve the repeated substring pattern problem using Python. You can find many more practice questions for coding interviews solved and explained using Python here.

Summary

In the Repeated Substring Pattern problem, you will be given a string. To solve this problem, you need to check if the given string can be formed by repeating a substring of the string. I hope you liked this article on how to solve the Repeated Substring Pattern problem using Python. Feel free to ask valuable questions in the comments section below.

Aman Kharwal
Aman Kharwal

I'm a writer and data scientist on a mission to educate others about the incredible power of data📈.

Articles: 1364

Leave a Reply