

- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
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
#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
- Related Questions & Answers
- Program to count number of common divisors of two numbers in Python
- Find the largest good number in the divisors of given number N in C++
- C++ Program for the Common Divisors of Two Numbers?
- Find sum of divisors of all the divisors of a natural number in C++
- Count all perfect divisors of a number in C++
- Program to find count of numbers having odd number of divisors in given range in C++
- Program to count the number of consistent strings in Python
- Count the number of operations required to reduce the given number in C++
- C++ Program for Common Divisors of Two Numbers?
- Python Program for Common Divisors of Two Numbers
- Java Program for Common Divisors of Two Numbers
- Common Character Count in Strings in JavaScript
- Count of sub-strings of length n possible from the given string in C++
- Count pairs of non-overlapping palindromic sub-strings of the given string in C++
- Greatest common divisors in Python