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.
Input: a = “xaxa”, b = “xaxaxaxa”
The common divisors are “xa” and “xaxa”
Input: a = “bbbb”, b = “bbb”
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:
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
- Count common characters in two strings
- Count common subsequence in two strings
- Number of common base strings for two strings
- Count of elements having odd number of divisors in index range [L, R] for Q queries
- Count the number of Special Strings of a given length N
- Count number of binary strings without consecutive 1’s : Set 2
- Count number of strings (made of R, G and B) using given combination
- Count the Number of matching characters in a pair of strings
- Count of binary strings of length N having equal count of 0's and 1's and count of 1's ≥ count of 0's in each prefix substring
- Count the number of strings in an array whose distinct characters are less than equal to M
- Count number of binary strings such that there is no substring of length greater than or equal to 3 with all 1's
- Common characters in n strings
- Check if two strings have a common substring
- Check if there is any common character in two given strings
- LCS (Longest Common Subsequence) of three strings
- Interleaving of two given strings with no common characters
- Longest Common Substring in an Array of Strings
- Longest common anagram subsequence from N strings
- Count number of rotated strings which have more number of vowels in the first half than second half
- Program to find Greatest Common Divisor (GCD) of N strings
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.