Check if a string can be repeated to make another string - GeeksforGeeks

13 May, 2021
Given two strings a and b, the task is to check how many times the string a can be repeated to generate the string b. If b cannot be generated by repeating a then print -1.Examples:  
Input: a = “geeks”, b = “geeksgeeks” Output: 2 “geeks” can be repeated twice to generate “geeksgeeks”Input: a = “df”, b = “dfgrt” Output: -1  
 
Approach:  
If len(b) % len(a) != 0 then print -1 as b cannot be generated by repeating a.
Else set count = len(b) / len(a) and repeat a count number of times. If a = b then print count.Else print -1.
If a = b then print count.
Else print -1.
Below is the implementation of the above approach:  
C++
Java
Python 3
C#
PHP
Javascript
// CPP implementation of the approach#include<bits/stdc++.h>using namespace std; // Function to return the count of repetitions// of string a to generate string bint getCount(string a, string b){     // If b cannot be generated by repeating a    if(b.length() % a.length() != 0)        return -1;         int count = b.length() /a.length();     // Repeat a count number of times    string str = "";    for(int i = 0; i < count; i++)    {        str = str + a;    }    if(str == b)        return count;         return -1;} // Driver codeint main(){    string a = "geeks";    string b = "geeksgeeks";    cout << (getCount(a, b));    return 0;} // This code is contributed by// Surendra_Gangwar
// Java implementation of the approach class GfG{     // Function to return the count of repetitions    // of string a to generate string b    static int getCount(String a, String b)    {        // If b cannot be generated by repeating a        if(b.length() % a.length() != 0)            return -1;                 int count = b.length() / a.length();             // Repeat a count number of times        String str = "";        for(int i = 0; i < count; i++)        {            str = str + a;        }                 if(str.equals(b))            return count;                 return -1;    }     // Driver code    public static void main(String []args)    {        String a = "geeks";        String b = "geeksgeeks";        System.out.println(getCount(a, b));    }}     // This code is contributed by Rituraj Jain
# Python3 implementation of the approach # Function to return the count of repetitions# of string a to generate string bdef getCount(a, b):         # If b cannot be generated by repeating a    if(len(b) % len(a) != 0):        return -1;         count = int(len(b) / len(a))         # Repeat a count number of times    a = a * count         if(a == b):        return count         return -1; # Driver codeif __name__ == '__main__':    a = 'geeks'    b = 'geeksgeeks'    print(getCount(a, b))
// C# implementation of the approachusing System; class GfG{     // Function to return the count of repetitions    // of string a to generate string b    static int getCount(String a, String b)    {        // If b cannot be generated by repeating a        if(b.Length % a.Length != 0)            return -1;                 int count = b.Length / a.Length;             // Repeat a count number of times        String str = "";        for(int i = 0; i < count; i++)        {            str = str + a;        }                 if(str.Equals(b))            return count;                 return -1;    }     // Driver code    public static void Main(String []args)    {        String a = "geeks";        String b = "geeksgeeks";        Console.WriteLine(getCount(a, b));    }} // This code contributed by Rajput-Ji
<?php// PHP implementation of the approach // Function to return the count of repetitions// of string a to generate string bfunction getCount($a, $b){         // If b cannot be generated by repeating a    if(strlen($b) % strlen($a) != 0)        return -1;         $count = floor(strlen($b) / strlen($a));         // Repeat a count number of times    // Repeat a count number of times    $str = "";    for($i = 0; $i < $count; $i++)    {        $str = $str . $a ;    }         if(strcmp($a,$b))        return $count;         return -1;} // Driver code$a = 'geeks';$b = 'geeksgeeks';echo getCount($a, $b); // This code is contributed by Ryuga?>
<script> // Javascript implementation of the approach      // Function to return the count of repetitions    // of string a to generate string b    function getCount( a,  b) {        // If b cannot be generated by repeating a        if (b.length % a.length != 0)            return -1;         var count = parseInt(b.length / a.length);         // Repeat a count number of times        var str = "";        for (i = 0; i < count; i++) {            str = str + a;        }         if (str == (b))            return count;         return -1;    }     // Driver code             var a = "geeks";        var b = "geeksgeeks";        document.write(getCount(a, b)); // This code is contributed by todaysgaurav </script>
2
 
SURENDRA_GANGWAR
ankthon
rituraj_jain
Rajput-Ji
todaysgaurav
Pattern Searching
Python Programs
Strings
Strings
Pattern Searching
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Minimize number of cuts required to break N length stick into N unit length sticks
How to validate HTML tag using Regular Expression
How to validate GUID (Globally Unique Identifier) using Regular Expression
Check whether two strings contain same characters in same order
How to check Aadhaar number is valid or not using Regular Expression
Python program to convert a list to string
Defaultdict in Python
Python | Split string into list of characters
Python | Get dictionary keys as a list
Python | Convert a list to dictionary