Check if a string can be repeated to make another string
Last Updated :
16 Sep, 2023
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++
#include<bits/stdc++.h>
using namespace std;
int getCount(string a, string b)
{
if (b.length() % a.length() != 0)
return -1;
int count = b.length() /a.length();
string str = "" ;
for ( int i = 0; i < count; i++)
{
str = str + a;
}
if (str == b)
return count;
return -1;
}
int main()
{
string a = "geeks" ;
string b = "geeksgeeks" ;
cout << (getCount(a, b));
return 0;
}
|
Java
class GfG
{
static int getCount(String a, String b)
{
if (b.length() % a.length() != 0 )
return - 1 ;
int count = b.length() / a.length();
String str = "" ;
for ( int i = 0 ; i < count; i++)
{
str = str + a;
}
if (str.equals(b))
return count;
return - 1 ;
}
public static void main(String []args)
{
String a = "geeks" ;
String b = "geeksgeeks" ;
System.out.println(getCount(a, b));
}
}
|
Python 3
def getCount(a, b):
if ( len (b) % len (a) ! = 0 ):
return - 1 ;
count = int ( len (b) / len (a))
a = a * count
if (a = = b):
return count
return - 1 ;
if __name__ = = '__main__' :
a = 'geeks'
b = 'geeksgeeks'
print (getCount(a, b))
|
C#
using System;
class GfG
{
static int getCount(String a, String b)
{
if (b.Length % a.Length != 0)
return -1;
int count = b.Length / a.Length;
String str = "" ;
for ( int i = 0; i < count; i++)
{
str = str + a;
}
if (str.Equals(b))
return count;
return -1;
}
public static void Main(String []args)
{
String a = "geeks" ;
String b = "geeksgeeks" ;
Console.WriteLine(getCount(a, b));
}
}
|
Javascript
<script>
function getCount( a, b) {
if (b.length % a.length != 0)
return -1;
var count = parseInt(b.length / a.length);
var str = "" ;
for (i = 0; i < count; i++) {
str = str + a;
}
if (str == (b))
return count;
return -1;
}
var a = "geeks" ;
var b = "geeksgeeks" ;
document.write(getCount(a, b));
</script>
|
PHP
<?php
function getCount( $a , $b )
{
if ( strlen ( $b ) % strlen ( $a ) != 0)
return -1;
$count = floor ( strlen ( $b ) / strlen ( $a ));
$str = "" ;
for ( $i = 0; $i < $count ; $i ++)
{
$str = $str . $a ;
}
if ( strcmp ( $a , $b ))
return $count ;
return -1;
}
$a = 'geeks' ;
$b = 'geeksgeeks' ;
echo getCount( $a , $b );
?>
|
Time Complexity: O(n/m), where m and n are the lengths of the given strings a and b respectively.
Auxiliary Space: O(n), where n is the length of the string.
Approach:
- Check if the length of string b is a multiple of the length of string a. If not, return -1.
- Calculate the number of repetitions of a needed to generate b.
- Create an empty stack.
- Push each substring of length n (i.e., the length of a) from b onto the stack. This is done by iterating from 0 to the number of repetitions needed, and calling the substr() function on b with the starting index i*n and length n.
- Pop each substring from the stack and append it to a new string str. This is done by iterating until the stack is empty, calling the top() function to get the top element of the stack, concatenating it to str, and then calling the pop() function to remove the top element.
- Check if str is equal to b. If it is, return the number of repetitions calculated in step 2; otherwise, return -1.
Below is the implementation of the above approach:
C++
#include <iostream>
#include <stack>
using namespace std;
int count_repetitions(string a, string b) {
int n = a.length();
int m = b.length();
if (m % n != 0) {
return -1;
}
int repetitions = m / n;
stack<string> s;
for ( int i = 0; i < repetitions; i++) {
s.push(b.substr(i*n, n));
}
string str = "" ;
while (!s.empty()) {
str += s.top();
s.pop();
}
if (str == b) {
return repetitions;
} else {
return -1;
}
}
int main() {
string a = "geeks" ;
string b = "geeksgeeks" ;
cout << count_repetitions(a, b) << endl;
return 0;
}
|
Java
import java.util.*;
public class Main {
public static void main(String[] args) {
String a = "geeks" ;
String b = "geeksgeeks" ;
System.out.println(countRepetitions(a, b));
}
public static int countRepetitions(String a, String b) {
int n = a.length();
int m = b.length();
if (m % n != 0 ) {
return - 1 ;
}
int repetitions = m / n;
Stack<String> stack = new Stack<>();
for ( int i = 0 ; i < repetitions; i++) {
stack.push(b.substring(i * n, (i + 1 ) * n));
}
StringBuilder str = new StringBuilder();
while (!stack.isEmpty()) {
str.append(stack.pop());
}
if (str.toString().equals(b)) {
return repetitions;
} else {
return - 1 ;
}
}
}
|
Python3
def count_repetitions(a, b):
n = len (a)
m = len (b)
if m % n ! = 0 :
return - 1
repetitions = m / / n
stack = []
for i in range (repetitions):
stack.append(b[i * n: (i + 1 ) * n])
str_result = ""
while stack:
str_result + = stack.pop()
if str_result = = b:
return repetitions
else :
return - 1
if __name__ = = "__main__" :
a = "geeks"
b = "geeksgeeks"
print (count_repetitions(a, b))
|
C#
using System;
public class GFG
{
public static int CountRepetitions( string a, string b)
{
int n = a.Length;
int m = b.Length;
if (m % n != 0)
{
return -1;
}
int repetitions = m / n;
var stack = new System.Collections.Generic.Stack< string >();
for ( int i = 0; i < repetitions; i++)
{
stack.Push(b.Substring(i * n, n));
}
string strResult = "" ;
while (stack.Count > 0)
{
strResult += stack.Pop();
}
if (strResult == b)
{
return repetitions;
}
else
{
return -1;
}
}
public static void Main( string [] args)
{
string a = "geeks" ;
string b = "geeksgeeks" ;
Console.WriteLine(CountRepetitions(a, b));
}
}
|
Javascript
function countRepetitions(a, b) {
const n = a.length;
const m = b.length;
if (m % n !== 0) {
return -1;
}
const repetitions = m / n;
const stack = [];
for (let i = 0; i < repetitions; i++) {
stack.push(b.substring(i * n, (i + 1) * n));
}
let strResult = "" ;
while (stack.length > 0) {
strResult += stack.pop();
}
if (strResult === b) {
return repetitions;
} else {
return -1;
}
}
const a = "geeks" ;
const b = "geeksgeeks" ;
console.log(countRepetitions(a, b));
|
Time Complexity: O(m), where m is the length of the string b.
Auxiliary Space: O(m/n + m), where n is the length of the string a and m is the length of the string b.
Share your thoughts in the comments
Please Login to comment...