Skip to content
Programming101
Programming101

Learn everything about programming

  • Home
  • CS Subjects
    • IoT – Internet of Things
    • Digital Communication
    • Human Values
  • Programming Tutorials
    • C Programming
    • Data structures and Algorithms
  • HackerRank Solutions
    • HackerRank Algorithms Solutions
    • HackerRank C problems solutions
    • HackerRank C++ problems solutions
    • HackerRank Java problems solutions
    • HackerRank Python problems solutions
Programming101
Programming101

Learn everything about programming

Leetcode Repeated Substring Pattern problem solution

YASH PAL, 31 July 2024

In this Leetcode Repeated Substring Pattern problem solution, we have given a string s, check if it can be constructed by taking a substring of it and appending multiple copies of the substring together.

Leetcode Repeated Substring Pattern problem solution

Problem solution in Python.

class Solution:
    def repeatedSubstringPattern(self, s: str) -> bool:
        for i in range(2, len(s) // 2 + 1):
            if len(s) % i != 0:
                continue
                
            ss = s[:i]
            for j in range(i, len(s), i):
                if s[j:j+i] != ss:
                    break
            else:
                return True
                    
        return False

Problem solution in Java.

static boolean repeatedSubstringPattern(String s) {

        int end_partition = 1;
        if(s == null || s.length() <= 1) return false;

        while(end_partition <= s.length() - 1) {

            String substring = s.substring(0, end_partition);
            int substring_length = substring.length();

            if(s.length() % substring_length == 0) {

                int susbtring_in_string = s.length() / substring_length;
                StringBuilder sb = new StringBuilder();
                for(int i = 0; i < susbtring_in_string; i++) {
                    sb.append(substring);
                }

                if(s.equals(sb.toString())) {
                    return  true;
                }
            }
            end_partition++;
        }

        return false;
    }

Problem solution in C++.

class Solution {
public:
    bool repeatedSubstringPattern(string s) {
    string str=s+s;
        str.pop_back();
        str=str.substr(1);
        if(str.find(s)!=string::npos)
            return true;
        return false;
    }
};

Problem solution in C.

bool judge_even(char* str, int len) {
    int i = len / 2;
    int j = 0;
    while(j < i) {
        if(str[j] != str[i+j])
            return false;
        ++j;
    }
    return true;
}

bool judge_odd(char* str, int len) {
    int i = len / 3;
    int j = 2 * i;
    int k = 0;
    while(k < i) {
        if(str[k] != str[i+k] || str[k] != str[j+k])
            return false;
        ++k;
    }
    return true;
}

bool judge_prime(char* str, int len) {
    char pre = str[0];
    int i;
    for(i = 1; i < len; ++i) {
        if(str[i] != pre)
            return false;
        pre = str[i];
    }
    return true;
}

bool repeatedSubstringPattern(char* str) {
    int len = strlen(str);
    
    if(1 == len) return false;
    
    if(len % 3 == 0 && len % 2 == 0) {
        if(judge_db3(str, len)) return true;
        return judge_even(str, len);
    }
    
    if(len % 3 == 0) {
        return judge_db3(str, len);
    }
    
    if(len % 2 == 0) {
        return judge_even(str, len);
    }
    
    return judge_prime(str, len);
}

coding problems

Post navigation

Previous post
Next post
  • HackerRank Separate the Numbers solution
  • How AI Is Revolutionizing Personalized Learning in Schools
  • GTA 5 is the Game of the Year for 2024 and 2025
  • Hackerrank Day 5 loops 30 days of code solution
  • Hackerrank Day 6 Lets Review 30 days of code solution
©2025 Programming101 | WordPress Theme by SuperbThemes