In this post, we are going to solve Divisible Strings DSA Problem from Flipkart Online assessment. Let’s have a look at the problem statement first and then try to solve the problem.
Divisible Strings DSA Problem Statement
For two strings
and
, we say “T divides S” if and only if
=
+ … +
(i.e.,
is concatenated with itself one or more times).
Given two strings
and
, return the largest string
such that
divides both
and
.
Input
The first line of input consists of string
of
and consisting of uppercase latin letters only
The second line of input consists of string
of
and consisting of uppercase latin letters only
Output
Output
as described in the statement and if the answer does not exist you can output empty string.
Examples
Input
ABCABC ABC
Output
ABC
Input
ABABAB ABAB
Output
AB
Input
ABHCHDBHC AJBDHBCH
Output
Problem Solution in C++
#include <bits/stdc++.h>
using namespace std;
int main() {
string s,t;
cin>>s>>t;
int x=__gcd(s.size(),t.size());
if(s+t!=t+s)
{
cout<<"";
}
else
cout<<t.substr(0,x);
return 0;
}
Leave a Reply