Count the number of common divisors of the given strings | C # implementation of the above approach ; Function that returns true if sub - string s [ 0. . . k ] is repeated a number of times to generate String s ; Function to return the count of common divisors ; If the length of the sub - string divides length of both the strings ; If prefixes match in both the strings ; If both the strings can be generated ; Driver code
using System ; class GFG { static bool check ( string s , int k ) { for ( int i = 0 ; i < s . Length ; i ++ ) { if ( s [ i ] != s [ i % k ] ) { return false ; } } return true ; } static int countCommonDivisors ( string a , string b ) { int ct = 0 ; int n = a . Length , m = b . Length ; for ( int i = 1 ; i <= Math . Min ( n , m ) ; i ++ ) { if ( n % i == 0 && m % i == 0 ) { if ( a . Substring ( 0 , i ) == ( b . Substring ( 0 , i ) ) ) { if ( check ( a , i ) && check ( b , i ) ) { ct ++ ; } } } } return ct ; } public static void Main ( ) { string a = " xaxa " , b = " xaxaxaxa " ; Console . WriteLine ( countCommonDivisors ( a , b ) ) ; } }