Number of common base strings for two strings
Last Updated :
27 Nov, 2022
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.
Examples:
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
C++
// CPP program to count common base strings
// of s1 and s2.
#include <bits/stdc++.h>
using namespace std;
// function for finding common divisor .
bool isCommonBase(string base, string s1,
string s2)
{
// Checking if 'base' is base string
// of 's1'
for (int j = 0; j < s1.length(); ++j)
if (base[j % base.length()] != s1[j])
return false;
// Checking if 'base' is base string
// of 's2'
for (int j = 0; j < s2.length(); ++j)
if (base[j % base.length()] != s2[j])
return false;
return true;
}
int countCommonBases(string s1, string s2) {
int n1 = s1.length(), n2 = s2.length();
int count = 0;
for (int i=1; i<=min(n1, n2); i++)
{
string base = s1.substr(0, i);
if (isCommonBase(base, s1, s2))
count++;
}
return count;
}
// Driver code
int main()
{
string s1 = "pqrspqrs";
string s2 = "pqrspqrspqrspqrs";
cout << countCommonBases(s1, s2) << endl;
return 0;
}
Java
// Java program to count common
// base strings of s1 and s2.
class GFG
{
// function for finding common divisor
static boolean isCommonBase(String base,
String s1,
String s2)
{
// Checking if 'base' is base
// String of 's1'
for (int j = 0; j < s1.length(); ++j)
{
if (base.charAt(j %
base.length()) != s1.charAt(j))
{
return false;
}
}
// Checking if 'base' is base
// String of 's2'
for (int j = 0; j < s2.length(); ++j)
{
if (base.charAt(j %
base.length()) != s2.charAt(j))
{
return false;
}
}
return true;
}
static int countCommonBases(String s1,
String s2)
{
int n1 = s1.length(),
n2 = s2.length();
int count = 0;
for (int i = 1; i <= Math.min(n1, n2); i++)
{
String base = s1.substring(0, i);
if (isCommonBase(base, s1, s2))
{
count++;
}
}
return count;
}
// Driver code
public static void main(String[] args)
{
String s1 = "pqrspqrs";
String s2 = "pqrspqrspqrspqrs";
System.out.println(countCommonBases(s1, s2));
}
}
// This code is contributed by Rajput-JI
Python 3
# Python 3 program to count common
# base strings of s1 and s2.
# function for finding common divisor .
def isCommonBase(base, s1, s2):
# Checking if 'base' is base
# string of 's1'
for j in range(len(s1)):
if (base[j % len(base)] != s1[j]):
return False
# Checking if 'base' is base
# string of 's2'
for j in range(len(s2)):
if (base[j % len( base)] != s2[j]):
return False
return True
def countCommonBases(s1, s2):
n1 = len(s1)
n2 = len(s2)
count = 0
for i in range(1, min(n1, n2) + 1):
base = s1[0: i]
if (isCommonBase(base, s1, s2)):
count += 1
return count
# Driver code
if __name__ == "__main__":
s1 = "pqrspqrs"
s2 = "pqrspqrspqrspqrs"
print(countCommonBases(s1, s2))
# This code is contributed by ita_c
C#
// C# program to count common base
// strings of s1 and s2.
using System;
class GFG
{
// function for finding common divisor
static bool isCommonBase(String Base,
String s1,
String s2)
{
// Checking if 'base' is base
// String of 's1'
for (int j = 0; j < s1.Length; ++j)
{
if (Base[j % Base.Length] != s1[j])
{
return false;
}
}
// Checking if 'base' is base
// String of 's2'
for (int j = 0; j < s2.Length; ++j)
{
if (Base[j % Base.Length] != s2[j])
{
return false;
}
}
return true;
}
static int countCommonBases(String s1,
String s2)
{
int n1 = s1.Length, n2 = s2.Length;
int count = 0;
for (int i = 1;
i <= Math.Min(n1, n2); i++)
{
String Base = s1.Substring(0, i);
if (isCommonBase(Base, s1, s2))
{
count++;
}
}
return count;
}
// Driver code
public static void Main()
{
String s1 = "pqrspqrs";
String s2 = "pqrspqrspqrspqrs";
Console.Write(countCommonBases(s1, s2));
}
}
// This code is contributed by Rajput-JI
PHP
<?php
// PHP program to count common base strings
// of s1 and s2.
// function for finding common divisor .
function isCommonBase($base,$s1, $s2)
{
// Checking if 'base' is base string
// of 's1'
for ($j = 0; $j < strlen($s1); ++$j)
if ($base[$j % strlen($base)] != $s1[$j])
return false;
// Checking if 'base' is base string
// of 's2'
for ($j = 0; $j < strlen($s2); ++$j)
if ($base[$j % strlen($base)] != $s2[$j])
return false;
return true;
}
function countCommonBases($s1, $s2)
{
$n1 = strlen($s1);
$n2 = strlen($s2);
$count = 0;
for ($i = 1; $i <= min($n1, $n2); $i++)
{
$base = substr($s1,0, $i);
if (isCommonBase($base, $s1, $s2))
$count++;
}
return $count;
}
// Driver code
$s1 = "pqrspqrs";
$s2 = "pqrspqrspqrspqrs";
echo countCommonBases($s1, $s2)."\n";
// This code is contributed by mits
?>
JavaScript
<script>
// Javascript program to count common
// base strings of s1 and s2.
// function for finding common divisor
function isCommonBase(base,s1,s2)
{
// Checking if 'base' is base
// String of 's1'
for (let j = 0; j < s1.length; ++j)
{
if (base[j % base.length] != s1[j])
{
return false;
}
}
// Checking if 'base' is base
// String of 's2'
for (let j = 0; j < s2.length; ++j)
{
if (base[j %base.length] != s2[j])
{
return false;
}
}
return true;
}
function countCommonBases(s1,s2)
{
let n1 = s1.length,
n2 = s2.length;
let count = 0;
for (let i = 1; i <= Math.min(n1, n2); i++)
{
let base = s1.substring(0, i);
if (isCommonBase(base, s1, s2))
{
count++;
}
}
return count;
}
// Driver code
let s1 = "pqrspqrs";
let s2 = "pqrspqrspqrspqrs";
document.write(countCommonBases(s1, s2));
// This code is contributed by rag2127
</script>
Time Complexity: O(min(n1, n2) * (n1 + n2))
Auxiliary Space: O(min(n1, n2)), where n1 and n2 are the lengths of the given strings.
Similar Reads
Check if two strings have a common substring You are given two strings str1 and str2. You have to check if the two strings share a common substring. Examples : Input : str1 = "HELLO" str2 = "WORLD" Output : YES Explanation : The substrings "O" and "L" are common to both str1 and str2 Input : str1 = "HI" str2 = "ALL" Output : NO Explanation : B
5 min read
Count common characters in two strings Given two strings s1 and s2 consisting of lowercase English alphabets, the task is to count all the pairs of indices (i, j) from the given strings such that s1[i] = s2[j] and all the indices are distinct i.e. if s1[i] pairs with some s2[j] then these two characters will not be paired with any other
5 min read
Count number of equal pairs in a string Given a string s, find the number of pairs of characters that are same. Pairs (s[i], s[j]), (s[j], s[i]), (s[i], s[i]), (s[j], s[j]) should be considered different. Examples : Input: airOutput: 3Explanation :3 pairs that are equal are (a, a), (i, i) and (r, r)Input : geeksforgeeksOutput : 31Recommen
8 min read
Count the number of common divisors of the given strings 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. Examples: Input: a = "xaxa", b = "xaxaxaxa" Output: 2 The common divisors are "xa" and "xaxa" Input: a = "bbb
6 min read
Print common characters of two Strings in alphabetical order Given two strings, print all the common characters in lexicographical order. If there are no common letters, print -1. All letters are lower case. Examples: Input : string1 : geeks string2 : forgeeks Output : eegks Explanation: The letters that are common between the two strings are e(2 times), k(1
6 min read