Check if a string can be repeated to make another string
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.
Below is the implementation of the above approach:
C++
// 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 b 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 == b) return count; return -1; } // Driver code int main() { string a = "geeks"; string b = "geeksgeeks"; cout << (getCount(a, b)); return 0; } // This code is contributed by // Surendra_Gangwar |
chevron_right
filter_none
Java
// 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 |
chevron_right
filter_none
Python 3
# Python3 implementation of the approach # Function to return the count of repetitions # of string a to generate string b def 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 code if __name__ == '__main__': a = 'geeks' b = 'geeksgeeks' print(getCount(a, b)) |
chevron_right
filter_none
C#
// C# implementation of the approach using 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 |
chevron_right
filter_none
PHP
<?php // PHP 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(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 ?> |
chevron_right
filter_none
Output:
2
Recommended Posts:
- Longest sub string of 0's in a binary string which is repeated K times
- Minimum changes required to make first string substring of second string
- String slicing in Python to check if a string can become empty by recursive deletion
- Python | Check if given string can be formed by concatenating string elements of list
- Check if a string can be converted to another string by replacing vowels and consonants
- String which when repeated exactly K times gives a permutation of S
- Find the first repeated word in a string
- Queries for characters in a repeated string
- Find the first repeated character in a string
- Check if a string can be obtained by rotating another string d places
- Python | Check if string ends with any string in given list
- Queries to check if string B exists as substring in string A
- Check if a string can be obtained by rotating another string 2 places
- Check if a string can become empty by recursively deleting a given sub-string
- Count occurrences of a character in a repeated string
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

