给定两个字符串S1和S2选自N和M小写字符,任务是通过重复地从任一字符串的附加的第一个字符从所选字符串构建字典顺序最大字符串并删除字符。
例子:
Input: S1 = “dbcbb”, S2 = “cdbbb”
Output: “dcdbcbbbbb”
Explanation:
Let ans be the lexicographically largest string which is initially empty and perform the following steps to generate the resultant string:
Take first character from s1: ans = “d”, s1 = “bcbb”, s2 = “cdbbb”
Take first character from s2: ans = “dc”, s1 = “bcbb”, word2 = “dbbb”
Take first character from s2: ans = “dcd”, s1 = “bcbb”, word2 = “bbb”
Take first character from s1: ans = “dcdb”, s1 = “cbb”, word2 = “bbb”
Take first character from s1: ans = “dcbdc”, s1 = “bb”, word2 = “bbb”
Append the remaining 5 b’s from s1 and s2 at the end of ans. Therefore, print “dcdbcbbbbb” as the resultant string.
Input: S1 = “xyzxyz”, S2 = “xywzxyx”
Output: “xyzxyzxywzxyx”
方法:给定的问题可以通过使用两点方法来解决。请按照以下步骤解决问题:
- 初始化一个空字符串,比如合并为“”来存储字典序最大的字符串。
- 初始化两个指针,比如i为0 , j为0以同时遍历两个字符串。
- 遍历字符串直到其中一个字符串被完全使用。
- 如果子串word1[i, N – 1]在字典序上大于或等于子串word2[j, M – 1] ,则在字符串合并的末尾附加字符word1[i]并将指针i增加1 .
- 否则,在字符串合并的末尾附加字符word2[i]并将指针j增加1 。
- 完成上述步骤后,将字符串合并打印为结果字符串。
下面是上述方法的实现:
C++
// C++ program for the above approach
#include
using namespace std;
// Function to make the lexicographically
// largest string by merging two strings
string largestMerge(string word1,
string word2)
{
// Stores the resultant string
string merge = "";
while (word1.size() != 0
|| word2.size() != 0) {
// If the string word1 is
// lexographically greater
// than or equal to word2
if (word1 >= word2) {
// Update the string merge
merge = merge + word1[0];
// Erase the first index
// of the string word1
word1.erase(word1.begin() + 0);
}
// Otherwise
else {
// Update the string merge
merge = merge + word2[0];
// Erase the first index of
// the string word2
word2.erase(word2.begin() + 0);
}
}
// Return the final string
return merge;
}
// Driver Code
int main()
{
string S1 = "xyzxyz";
string S2 = "xywzxyx";
cout << largestMerge(S1, S2);
return 0;
}
Java
// Java program for the above approach
import java.io.*;
class GFG {
// Function to make the lexicographically
// largest string by merging two strings
static String largestMerge(String word1,
String word2)
{
// Stores the resultant string
String merge = "";
while (word1.length() != 0 ||
word2.length() != 0)
{
// If the string word1 is
// lexographically greater
// than or equal to word
if (word1.compareTo(word2) == 0 || ( word1.compareTo(word2) > 0))
{
// Update the string merge
merge = merge + word1.charAt(0);
// Erase the first index
// of the string word1
word1 = word1.substring(1);
}
// Otherwise
else
{
// Update the string merge
merge = merge + word2.charAt(0);
// Erase the first index of
// the string word2
word2 = word2.substring(1);
}
}
// Return the final string
return merge;
}
// Driver Code
public static void main(String[] args)
{
String S1 = "xyzxyz";
String S2 = "xywzxyx";
System.out.println(largestMerge(S1, S2));
}
}
// This code is contributed by sanjoy_62.
Python3
# Python program for the above approach
# Function to make the lexicographically
# largest string by merging two strings
def largestMerge(word1, word2):
# Stores the resultant string
merge = ""
while len(word1) != 0 or len(word2) != 0:
# If the string word1 is
# lexographically greater
# than or equal to word2
if word1 >= word2:
# Update the string merge
merge = merge + word1[0]
# Erase the first index
# of the string word1
word1 = word1[1:]
# Otherwise
else:
# Update the string merge
merge = merge + word2[0]
# Erase the first index
# of the string word2
word2 = word2[1:]
# Return the final string
return merge
# Driver code
S1 = "xyzxyz"
S2 = "xywzxyx"
print(largestMerge(S1, S2))
# This code is contributed by Parth Manchanda
C#
// C# program for the above approach
using System;
using System.Collections.Generic;
class GFG{
// Function to make the lexicographically
// largest string by merging two strings
static string largestMerge(string word1,
string word2)
{
// Stores the resultant string
string merge = "";
while (word1.Length != 0 ||
word2.Length != 0)
{
// If the string word1 is
// lexographically greater
// than or equal to word2
if (String.Compare(word1, word2) == 0 ||
String.Compare(word1, word2) > 0)
{
// Update the string merge
merge = merge + word1[0];
// Erase the first index
// of the string word1
word1 = word1.Substring(1);
}
// Otherwise
else
{
// Update the string merge
merge = merge + word2[0];
// Erase the first index of
// the string word2
word2 = word2.Substring(1);
}
}
// Return the final string
return merge;
}
// Driver Code
public static void Main()
{
string S1 = "xyzxyz";
string S2 = "xywzxyx";
Console.Write(largestMerge(S1, S2));
}
}
// This code is contributed by SURENDRA_GANGWAR
xyzxyzxywzxyx
时间复杂度: O(N*M)
辅助空间: O(1)
如果您希望与专家一起参加现场课程,请参阅DSA 现场工作专业课程和学生竞争性编程现场课程。