Count the number of common divisors of the given strings | Java implementation of the 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
class GFG { static boolean check ( String s , int k ) { for ( int i = 0 ; i < s . length ( ) ; i ++ ) { if ( s . charAt ( i ) != s . charAt ( 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 ) . equals ( b . substring ( 0 , i ) ) ) { if ( check ( a , i ) && check ( b , i ) ) { ct ++ ; } } } } return ct ; } public static void main ( String [ ] args ) { String a = " xaxa " , b = " xaxaxaxa " ; System . out . println ( countCommonDivisors ( a , b ) ) ; } }