Count the number of common divisors of the given strings in C++


Given two strings numo and demo as input. The goal is to find the number of common divisors of both the strings. The divisors of a string are found using following technique: If string str has sub1 as its divisor then we can construct str using sub1 by repeating it any number of times till str is generated. Example: str=abcabcabc sub1=abc

For Example

Input

numo = "abababab" demo = "abababababababab"

Output

Count of number of common divisors of the given strings are: 2

Explanation

The strings can be generated using following divisor substrings :
“ab”, “abab”

Input

numo = "pqqppqqp" demo = "pqpq"

Output

Count of number of common divisors of the given strings are: 0

Explanation

The strings do not have any common divisor. Only divisors of both are:
“pqqp” for numo and “pq” for demo.

Approach used in the below program is as follows

For any string sub1 to be divisor of str, it must be a prefix of str and its length must fully divide the length of str. Check this condition of sub1 with both strings numo and demo and increment count accordingly.

  • Take strings numo and demo as input.

  • Function verify(string str, int val) takes string str and returns 1 if substring between 0 to val can be repeated to generate str.

  • Function common_divisor(string numo, string demo) takes both strings and returns a count of the number of common divisors of the given strings.

  • Take the initial count as 0.

  • Calculate lengths of input strings. And store minimum length in min_val.

  • Traverse using for loop from index i=0 to min_val.

  • If current length i of substring divides lengths of both strings numo_size and demo_size and prefixes also match numo.substr(0, i) == demo.substr(0, i).

  • Then find if substring 0 to i is divisor of both numo and demo using verify()

  • If both verify(numo,i) and verify(demo,i) returns 1 then increment count.

  • At the end of for loop returns count as result.

Example

 Live Demo

#include <bits/stdc++.h>
using namespace std;
int verify(string str, int val){
   int length = str.length();
   for (int i = 0; i < length; i++){
      if(str[i] != str[i % val]){
         return 0;
      }
   }
   return 1;
}
int common_divisor(string numo, string demo){
   int count = 0;
   int numo_size = numo.size();
   int demo_size = demo.size();
   int min_val = min(numo_size, demo_size);
   for(int i = 1; i <= min_val; i++){
      if(numo_size % i == 0){
         if(demo_size % i == 0){
            if(numo.substr(0, i) == demo.substr(0, i)){
               if(verify(numo, i)==1){
                  if(verify(demo, i)==1){
                     count++;
                  }
               }
            }
         }
      }
   }
   return count;
}
int main(){
   string numo = "abababab";
   string demo = "abababababababab";
   cout<<"Count the number of common divisors of the given strings are:
   "<<common_divisor(numo, demo);
   return 0;
}

Output

If we run the above code it will generate the following output −

Count the number of common divisors of the given strings are: 3

Updated on: 05-Jan-2021

163 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements