📜  查找给定字符串的最小公倍数 (LCM)

📅  最后修改于: 2022-05-13 01:56:09.109000             🧑  作者: Mango

查找给定字符串的最小公倍数 (LCM)

给定两个长度分别为NM的字符串str1str2 。任务是找到两个字符串的最小公倍数(LCM) ,如果不存在则打印-1

注意:两个字符串的 LCM 是一个字符串,可以通过将字符串连接到自己来形成,并且长度最短。

例子:

方法:这个问题类似于寻找由连接 A x 次和 B y 次形成的最短字符串。通过使两个字符串的长度相等然后检查两个字符串是否相同来解决该任务。请按照以下步骤解决问题:

  • 迭代直到两个字符串的长度不相等。
  • 如果str1的长度小于str2 ,则将str1附加到自身,否则对str2执行相同操作。
  • 在循环之后,如果两者相等,则打印任何字符串,否则打印 -1。

下面是上述方法的实现:

C++
// C++ program for the above approach
#include 
using namespace std;
 
// Function to find the lcm of both strings
string findLcm(string str1, string str2)
{
    string x = str1, y = str2;
 
    // While their length is unequal
    while (x.length() != y.length()) {
        if (x.length() < y.length())
            x += str1;
        else
            y += str2;
    }
 
    if (x == y)
        return x;
    return "";
}
 
// Driver Code
int main()
{
    string str1 = "ba", str2 = "baba";
 
    string ans = findLcm(str1, str2);
    if (ans != "")
        cout << ans;
    else {
        cout << -1;
    }
    return 0;
}


Java
// Java program for the above approach
import java.util.*;
class GFG{
 
  // Function to find the lcm of both Strings
  static String findLcm(String str1, String str2)
  {
    String x = str1, y = str2;
 
    // While their length is unequal
    while (x.length() != y.length()) {
      if (x.length() < y.length())
        x += str1;
      else
        y += str2;
    }
 
    if (x.equals(y))
      return x;
    return "";
  }
 
  // Driver Code
  public static void main(String[] args)
  {
    String str1 = "ba", str2 = "baba";
 
    String ans = findLcm(str1, str2);
    if (ans != "")
      System.out.print(ans);
    else {
      System.out.print(-1);
    }
  }
}
 
// This code is contributed by shikhasingrajput


Python3
# Python code for the above approach
 
# Function to find the lcm of both strings
def findLcm(str1, str2):
    x = str1
    y = str2
 
    # While their length is unequal
    while (len(x) != len(y)):
        if (len(x) < len(y)):
            x += str1;
        else:
            y += str2;
 
    if (x == y):
        return x;
    return "";
 
# Driver Code
str1 = "ba"
str2 = "baba"
 
ans = findLcm(str1, str2)
if (ans != ""):
    print(ans);
else:
    print(-1);
 
# This code is contributed by gfgking


C#
// C# program for the above approach
using System;
class GFG
{
  // Function to find the lcm of both strings
  static string findLcm(string str1, string str2)
  {
    string x = str1, y = str2;
 
    // While their length is unequal
    while (x.Length != y.Length) {
      if (x.Length < y.Length)
        x += str1;
      else
        y += str2;
    }
 
    if (x == y)
      return x;
    return "";
  }
 
  // Driver Code
  public static void Main()
  {
    string str1 = "ba", str2 = "baba";
 
    string ans = findLcm(str1, str2);
    if (ans != "")
      Console.Write(ans);
    else {
      Console.Write(-1);
    }
  }
}
 
// This code is contributed by Samim Hossain Mondal.


Javascript



输出
baba

时间复杂度:O(N + M)
辅助空间:O(N + M)