0

I have to find the LCM of 2 strings given that a multiplication operation exists between a number and a string which dictates that if x is a number and s is a string, then s*x is equal to the string s taken x times.

the strings given are "baba" and "ba". Now I am having trouble even getting started with this because I have to print -1 if the LCM does not exist.

Please help with this. I am just a beginner in c++.

Mohit Singh
  • 143
  • 11
  • 3
    Show your code and clearly indicate what you have problems with and someone may be able to help you. – Ted Lyngmo Jan 14 '21 at 15:53
  • 2
    what help do you need? What did you try? Is your code not doing what you want it to do? Do you have any code? [Why is “Can someone help me?” not an actual question?](https://meta.stackoverflow.com/questions/284236/why-is-can-someone-help-me-not-an-actual-question) – 463035818_is_not_an_ai Jan 14 '21 at 15:53
  • I coded a function which appends the string s to itself n times and returns it – Mohit Singh Jan 14 '21 at 15:55
  • 1
    If this LCM exists, then its length is the LCM of the two lenghts. This may help... – Damien Jan 14 '21 at 15:57
  • Thank you! That helps. I will try to devise a solution and post the code here. – Mohit Singh Jan 14 '21 at 15:59
  • How can I check if the LCM does not exist – Mohit Singh Jan 14 '21 at 16:04
  • (Note: if you ask a question to someone use @Name). You know the size `n = lcm(n1, n2)` of the possible LCM string. You have to check that `s1[i%n1] == s2[i%n2]`, for all `i` from `0` to `n-1`, to check that this LCM exists. In other words, you have to check that `p*s1 == q*s2`, where `p*n1 = q*n2 = lcm(n1, n2)` – Damien Jan 14 '21 at 16:09
  • I am sorry @Damien, I forgot to tag you here. I found a solution which I tested for a number of test cases and was correct for all of them. I will post the answer here. – Mohit Singh Jan 14 '21 at 17:21

1 Answers1

1

The lcm of the two strings can be found by calculating the lcm of their lengths (Thank you @Damien for this).

int gcd(int a, int b){
    if (b==0)
    {
        return a;
    }
    return gcd(b,a%b);
}

int lcm(int a,int b){
    return (a/gcd(a,b))*b;
}

After you have the lcm of their lengths, then you append each string to itself until the strings are of the same length as the lcm of their lengths.

string StringLCM(string s,int n){
    string result = s;
    for (int i = 0; i < n-1; i++)
    {
        result.append(s);
    }
    return result;
}

THe StringLCM function returns the strings of length equal to the LCM. Now just compare the 2 strings and check if they are equal or not. THe program prints -1 if no LCM can be found. Below is the complete code.

#include<iostream>
using namespace std;

int gcd(int a, int b){
    if (b==0)
    {
        return a;
    }
    return gcd(b,a%b);
}

int lcm(int a,int b){
    return (a/gcd(a,b))*b;
}

string StringLCM(string s,int n){
    string result = s;
    for (int i = 0; i < n-1; i++)
    {
        result.append(s);
    }
    return result;
}
 
int main(){
    string s1 = "aba";
    string s2 = "ab";
    int n1= s1.length();
    int n2= s2.length();
    int l=lcm(n1,n2);  
    cout<<l<<endl;
    int flag=0;
    int e =-1;

    s1=StringLCM(s1,l/n1);
    cout<<s1<<endl;
    s2=StringLCM(s2,l/n2);
    cout<<s2<<endl;

    if (s1==s2)
    {
        flag=1;
    }
    if (flag==1)
    {
        cout<<s1<<endl;
    }else
    {
        cout<<e<<endl;
    }
    return 0;
}
Mohit Singh
  • 143
  • 11