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:
- Count common subsequence in two strings
- Count common characters in two strings
- Number of common base strings for two strings
- Count number of strings (made of R, G and B) using given combination
- 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
- Count number of rotated strings which have more number of vowels in the first half than second half
- LCS (Longest Common Subsequence) of three strings
- Interleaving of two given strings with no common characters
- Check if two strings have a common substring
- Check if there is any common character in two given strings
- Longest Common Substring in an Array of Strings
- Longest common anagram subsequence from N strings
- Count of numbers below N whose sum of prime divisors is K
- Print common characters of two Strings in alphabetical order
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. 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.