Dadas dos strings a y b , la tarea es comprobar cuántas veces se puede repetir la string a para generar la string b . Si b no se puede generar repitiendo a , imprima -1 .
Ejemplos:
Entrada: a = «geeks», b = «geeksgeeks»
Salida : 2
«geeks» se pueden repetir dos veces para generar «geeksgeeks»
Entrada: a = «df», b = «dfgrt»
Salida: -1
Acercarse:
- Si len(b) % len(a) != 0 entonces imprima -1 ya que b no se puede generar repitiendo a .
- De lo contrario, establezca el conteo = len(b) / len(a) y repita el conteo varias veces.
- Si a = b entonces imprima la cuenta .
- De lo contrario imprime -1 .
A continuación se muestra la implementación del enfoque anterior:
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
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
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))
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
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 ?>
Javascript
<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>
Producción:
2