给定两个字符串s和长度为N T和M,任务是找到是双方两个字符串分割最小的字符串。如果不存在这样的字符串,则打印-1 。
For any two strings A and B, B divides A if and only if A is the concatenation of B at least once.
例子:
Input: S = “abab”, T = “ab”
Output: abab
Explanation: The string “abab” is same as S and twice the concatenation of string T (“abab” = “ab” + “ab” = T + T)
Input: S = “ccc”, T = “cc”
Output: cccccc
Explanation: The string “cccccc” is concatenation of S and T twice and thrice respectively.
(“cccccc” = “ccc” + “ccc” = S + S)
(“cccccc” = “cc” + “cc” + “cc” = T + T + T)
方法:这个想法基于这样的观察,即所需字符串的长度,例如L,必须等于N和M 的最小公倍数。检查字符串S连接L / N次是否等于字符串T连接L / M次。如果发现是真的,打印其中任何一个。否则,打印-1 。请按照以下步骤解决问题:
- 将N和M的最小公倍数存储在一个变量中,比如L 。
- 初始化两个字符串S1和T1 。
- 将字符串, S1与字符串, S , (L/N)次数连接起来。
- 将字符串, T1与字符串, T , (L/M)次数连接起来。
- 如果字符串S1和T1相等,则打印S1作为结果。
- 否则,打印-1 。
下面是上述方法的实现:
C++
// C++ program for the above approach
#include
using namespace std;
// Function to calculate
// GCD of two numbers
int gcd(int a, int b)
{
if (b == 0)
return a;
return gcd(b, a % b);
}
// Function to calculate
// LCM of two numbers
int lcm(int a, int b)
{
return (a / gcd(a, b)) * b;
}
// Function to find the smallest string
// which is divisible by strings S and T
void findSmallestString(string s, string t)
{
// Store the length of both strings
int n = s.length(), m = t.length();
// Store LCM of n and m
int l = lcm(n, m);
// Temporary strings to store
// concatenated strings
string s1 = "", t1 = "";
// Concatenate s1 (l / n) times
for (int i = 0; i < l / n; i++) {
s1 += s;
}
// Concatenate t1 (l / m) times
for (int i = 0; i < l / m; i++) {
t1 += t;
}
// If s1 and t1 are equal
if (s1 == t1)
cout << s1;
// Otherwise, print -1
else
cout << -1;
}
// Driver Code
int main()
{
string S = "baba", T = "ba";
findSmallestString(S, T);
return 0;
}
Java
// Java program for above approach
import java.io.*;
class GFG
{
// Function to calculate
// GCD of two numbers
static int gcd(int a, int b)
{
if (b == 0)
return a;
return gcd(b, a % b);
}
// Function to calculate
// LCM of two numbers
static int lcm(int a, int b)
{
return (a / gcd(a, b)) * b;
}
// Function to find the smallest string
// which is divisible by strings S and T
static void findSmallestString(String s, String t)
{
// Store the length of both strings
int n = s.length(), m = t.length();
// Store LCM of n and m
int l = lcm(n, m);
// Temporary strings to store
// concatenated strings
String s1 = "", t1 = "";
// Concatenate s1 (l / n) times
for (int i = 0; i < l / n; i++) {
s1 += s;
}
// Concatenate t1 (l / m) times
for (int i = 0; i < l / m; i++) {
t1 += t;
}
// If s1 and t1 are equal
if (s1.equals(t1)){
System.out.println(s1);
}
// Otherwise, print -1
else{
System.out.println(-1);
}
}
// Driver code
public static void main(String[] args)
{
String S = "baba", T = "ba";
findSmallestString(S, T);
}
}
// This code is contributed by susmitakundugoaldanga.
Python3
# Python3 program for the above approach
# Function to calculate
# GCD of two numbers
def gcd(a, b):
if (b == 0):
return a
return gcd(b, a % b)
# Function to calculate
# LCM of two numbers
def lcm(a, b):
return (a // gcd(a, b)) * b
# Function to find the smallest string
# which is divisible by strings S and T
def findSmallestString(s, t):
# Store the length of both strings
n, m = len(s), len(t)
# Store LCM of n and m
l = lcm(n, m)
# Temporary strings to store
# concatenated strings
s1, t1 = "", ""
# Concatenate s1 (l / n) times
for i in range(l//n):
s1 += s
# Concatenate t1 (l / m) times
for i in range(l//m):
t1 += t
# If s1 and t1 are equal
if (s1 == t1):
print(s1)
# Otherwise, pr-1
else:
print(-1)
# Driver Code
if __name__ == '__main__':
S, T = "baba", "ba"
findSmallestString(S, T)
# This code is contributed by mohit kumar 29.
C#
// C# program for above approach
using System;
public class GFG
{
// Function to calculate
// GCD of two numbers
static int gcd(int a, int b)
{
if (b == 0)
return a;
return gcd(b, a % b);
}
// Function to calculate
// LCM of two numbers
static int lcm(int a, int b)
{
return (a / gcd(a, b)) * b;
}
// Function to find the smallest string
// which is divisible by strings S and T
static void findSmallestString(string s, string t)
{
// Store the length of both strings
int n = s.Length, m = t.Length;
// Store LCM of n and m
int l = lcm(n, m);
// Temporary strings to store
// concatenated strings
string s1 = "", t1 = "";
// Concatenate s1 (l / n) times
for (int i = 0; i < l / n; i++) {
s1 += s;
}
// Concatenate t1 (l / m) times
for (int i = 0; i < l / m; i++) {
t1 += t;
}
// If s1 and t1 are equal
if (s1 == t1)
Console.WriteLine(s1);
// Otherwise, print -1
else
Console.WriteLine(-1);
}
// Driver code
public static void Main(String[] args)
{
string S = "baba", T = "ba";
findSmallestString(S, T);
}
}
// This code is contributed by sanjoy_62.
baba
时间复杂度: O(max(N, M))
辅助空间: O(max(N, M))
如果您想与行业专家一起参加直播课程,请参阅Geeks Classes Live