Number of common base strings for two strings | C # program to count common base strings of s1 and s2 . ; function for finding common divisor ; Checking if ' base ' is base String of ' s1' ; Checking if ' base ' is base String of ' s2' ; Driver code
using System ; class GFG { static bool isCommonBase ( String Base , String s1 , String s2 ) { for ( int j = 0 ; j < s1 . Length ; ++ j ) { if ( Base [ j % Base . Length ] != s1 [ j ] ) { return false ; } } for ( int j = 0 ; j < s2 . Length ; ++ j ) { if ( Base [ j % Base . Length ] != s2 [ j ] ) { return false ; } } return true ; } static int countCommonBases ( String s1 , String s2 ) { int n1 = s1 . Length , n2 = s2 . Length ; int count = 0 ; for ( int i = 1 ; i <= Math . Min ( n1 , n2 ) ; i ++ ) { String Base = s1 . Substring ( 0 , i ) ; if ( isCommonBase ( Base , s1 , s2 ) ) { count ++ ; } } return count ; } public static void Main ( ) { String s1 = " pqrspqrs " ; String s2 = " pqrspqrspqrspqrs " ; Console . Write ( countCommonBases ( s1 , s2 ) ) ; } }