Count the number of common divisors of the given strings
Last Updated :
15 Sep, 2022
Given two strings a and b, the task is to count the number of common divisors of both the strings. A string s is a divisor of string t if t can be generated by repeating s a number of times.
Examples:
Input: a = “xaxa”, b = “xaxaxaxa”
Output: 2
The common divisors are “xa” and “xaxa”
Input: a = “bbbb”, b = “bbb”
Output:1
The only common divisor is “b”
Approach: For a string s to be a candidate divisor of string t, the following conditions must be fulfilled:
- s must be a prefix of t.
- len(t) % len(s) = 0
Initialize count = 0 and starting from the first character as the ending character of the prefix, check whether the length of the prefix divides the length of both the strings and also if the prefix is same in both the strings. If yes then update count = count + 1. Repeat these steps for all the possible prefixes. Print the value of count in the end.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int check(string s, int k)
{
for ( int i = 0; i < s.length(); i++)
if (s[i] != s[i % k])
return false ;
return true ;
}
int countCommonDivisors(string a, string b)
{
int ct = 0;
int n = a.size(), m = b.size();
for ( int i = 1; i <= min(n, m); i++) {
if (n % i == 0 && m % i == 0)
if (a.substr(0, i) == b.substr(0, i))
if (check(a, i) && check(b, i))
ct++;
}
return ct;
}
int main()
{
string a = "xaxa" , b = "xaxaxaxa" ;
cout << countCommonDivisors(a, b);
return 0;
}
|
Java
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));
}
}
|
Python3
def check(s, k):
for i in range ( 0 , len (s)):
if (s[i] ! = s[i % k]):
return False
return True
def countCommonDivisors(a, b):
ct = 0
n = len (a)
m = len (b)
for i in range ( 1 , min (n, m) + 1 ):
if (n % i = = 0 and m % i = = 0 ):
if (a[ 0 : i] = = b[ 0 : i]) :
if (check(a, i) and check(b, i)) :
ct = ct + 1
return ct
a = "xaxa"
b = "xaxaxaxa"
print (countCommonDivisors(a, b))
|
C#
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));
}
}
|
Javascript
<script>
function check(s, k)
{
for ( var i = 0; i < s.length; i++)
if (s[i] != s[i % k])
return false ;
return true ;
}
function countCommonDivisors(a, b)
{
var ct = 0;
var n = a.length, m = b.length;
for ( var 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;
}
var a = "xaxa" , b = "xaxaxaxa" ;
document.write( countCommonDivisors(a, b));
</script>
|
Complexity Analysis:
- Time Complexity: O(N * M)
- Auxiliary Space: O(1)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...