Given two strings s1 and s2, we need to find number of common base strings of two. A substring of a string s is called base string if repeated concatenation of the substring results in s.
Input : s1 = "pqrspqrs" s2 = "pqrspqrspqrspqrs" Output : 2 The two common base strings are "pqrs" and "pqrspqrs". Input: s1 = "bbb" s2 = "bb" Output: 1 There is only one common base string which is "b".
The maximum possible length of common base string is equal to length of smaller of two strings. So we run a loop that considers all prefixes of smaller string and for every prefix checks if it is a common base.
Below is the implementation of the following approach
Time Complexity : O(min(n1, n2) * (n1 + n2))
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 the number of common divisors of the given strings
- Common characters in n strings
- Count common characters in two strings
- Interleaving of two given strings with no common characters
- Check if there is any common character in two given strings
- LCS (Longest Common Subsequence) of three strings
- Check if two strings have a common substring
- Count common subsequence in two strings
- Longest common anagram subsequence from N strings
- Longest Common Substring in an Array of Strings
- Print common characters of two Strings in alphabetical order
- Python code to print common characters of two Strings in alphabetical order
- Meta Strings (Check if two strings can become same after a swap in one string)
- Search in an array of strings where non-empty strings are sorted
- Count of same length Strings that exists lexicographically in between two given Strings
- Count of strings that become equal to one of the two strings after one removal
- Pairs of complete strings in two sets of strings
- Distinct strings such that they contains given strings as sub-sequences
- Check whether two strings can be made equal by reversing substring of equal length from both strings
- Number of sub-strings that contain the given character exactly k times
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.