
- C++ Basics
- C++ Home
- C++ Overview
- C++ Environment Setup
- C++ Basic Syntax
- C++ Comments
- C++ Data Types
- C++ Variable Types
- C++ Variable Scope
- C++ Constants/Literals
- C++ Modifier Types
- C++ Storage Classes
- C++ Operators
- C++ Loop Types
- C++ Decision Making
- C++ Functions
- C++ Numbers
- C++ Arrays
- C++ Strings
- C++ Pointers
- C++ References
- C++ Date & Time
- C++ Basic Input/Output
- C++ Data Structures
- C++ Object Oriented
- C++ Classes & Objects
- C++ Inheritance
- C++ Overloading
- C++ Polymorphism
- C++ Abstraction
- C++ Encapsulation
- C++ Interfaces
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 Articles
- Program to count number of common divisors of two numbers in Python
- Program to find count of numbers having odd number of divisors in given range in C++
- Find the largest good number in the divisors of given number N in C++
- Count all perfect divisors of a number in C++
- C++ Program for the Common Divisors of Two Numbers?
- Count the numbers < N which have equal number of divisors as K in C++
- Program to count the number of consistent strings in Python
- Find sum of divisors of all the divisors of a natural number in C++
- Common Character Count in Strings in JavaScript
- Count divisors of n that have at-least one digit common with n in Java
- Count total divisors of A or B in a given range in C++
- Count number of strings (made of R, G and B) using given combination in C++
- Count common subsequence in two strings in C++
- Count common characters in two strings in C++
- Count divisors of array multiplication in C++
