📌  相关文章
📜  字典序最小的字符串可以通过合并两个排序的字符串

📅  最后修改于: 2021-09-06 06:21:40             🧑  作者: Mango

给定两个长度分别为NM 的排序字符串S1S2 ,任务是通过合并两个给定的字符串而不改变字符的出现顺序,按字典顺序构造可能的最小字符串。

例子:

朴素的方法:最简单的方法是将给定的两个字符串连接起来,并对结果字符串进行排序,以获得按字典顺序排列的最小字符串。

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

高效的方法:上述方法可以通过使用两点技术通过比较给定字符串的字符来优化,然后相应地构造所需的字符串。
请按照以下步骤解决问题:

  • 初始化两个指针,比如ptr1ptr2 ,分别指向字符串S1S2的开头。
  • 初始化一个字符串ans = “”,以存储结果字典中最小的字符串。
  • 迭代直到ptr1小于Nptr2小于M并执行以下步骤:
    • 如果S1[ptr1]小于S2[ptr2] ,则将字符S1[ptr1]附加到字符串ans 。增加ptr1
    • 否则,将字符S2[ptr2]附加到字符串ans 。增加ptr2
  • 完成上述步骤后,其中一个指针没有到达字符串的末尾。
  • 因此,将字符串的所有剩余字符添加到字符串ans的末尾。
  • 打印ans作为结果字符串。

下面是上述方法的实现:

C++14
// C++ program for the above approach
#include 
using namespace std;
 
// Function to find lexicographically
// smallest string possible by merging
// two sorted strings
void mergeStrings(string s1, string s2)
{
    // Stores length of string s1
    int len1 = s1.size();
 
    // Stores length of string s2
    int len2 = s2.size();
 
    // Pointer to beginning
    // of string1 i.e., s1
    int pntr1 = 0;
 
    // Pointer to beginning
    // of string2 i.e., s2
    int pntr2 = 0;
 
    // Stores the final string
    string ans = "";
 
    // Traverse the strings
    while (pntr1 < len1 && pntr2 < len2) {
 
        // Append the smaller of the
        // two current characters
        if (s1[pntr1] < s2[pntr2]) {
            ans += s1[pntr1];
            pntr1++;
        }
 
        else {
            ans += s2[pntr2];
            pntr2++;
        }
    }
 
    // Append the remaining characters
    // of any of the two strings
    if (pntr1 < len1) {
        ans += s1.substr(pntr1, len1);
    }
    if (pntr2 < len2) {
        ans += s2.substr(pntr2, len2);
    }
 
    // Print the final string
    cout << ans;
}
 
// Driver Code
int main()
{
    string S1 = "abdcdtx";
    string S2 = "achilp";
 
    // Function Call
    mergeStrings(S1, S2);
 
    return 0;
}


Java
// Java program for the above approach
import java.io.*;
 
class GFG{
     
// Function to find lexicographically
// smallest string possible by merging
// two sorted strings
static void mergeStrings(String s1, String s2)
{
 
    // Stores length of string s1
    int len1 = s1.length();
     
    // Stores length of string s2
    int len2 = s2.length();
     
    // Pointer to beginning
    // of string1 i.e., s1
    int pntr1 = 0;
     
    // Pointer to beginning
    // of string2 i.e., s2
    int pntr2 = 0;
     
    // Stores the final string
    String ans = "";
     
    // Traverse the strings
    while (pntr1 < len1 && pntr2 < len2)
    {
     
        // Append the smaller of the
        // two current characters
        if (s1.charAt(pntr1) < s2.charAt(pntr2))
        {
            ans += s1.charAt(pntr1);
            pntr1++;
        }
         
        else
        {
            ans += s2.charAt(pntr2);
            pntr2++;
        }
    }
     
    // Append the remaining characters
    // of any of the two strings
    if (pntr1 < len1)
    {
        ans += s1.substring(pntr1, len1);
    }
    if (pntr2 < len2)
    {
        ans += s2.substring(pntr2, len2);
    }
     
    // Print the final string
    System.out.println(ans);
}
 
// Driver Code
public static void main (String[] args)
{
    String S1 = "abdcdtx";
    String S2 = "achilp";
 
    // Function Call
    mergeStrings(S1, S2);
}
}
 
// This code is contributed by sanjoy_62


Python3
# Python3 program for the above approach
 
# Function to find lexicographically
# smallest possible by merging
# two sorted strings
def mergeStrings(s1, s2):
   
    # Stores length of s1
    len1 = len(s1)
 
    # Stores length of s2
    len2 = len(s2)
 
    # Pointer to beginning
    # of string1 i.e., s1
    pntr1 = 0
 
    # Pointer to beginning
    # of string2 i.e., s2
    pntr2 = 0
 
    # Stores the final string
    ans = ""
 
    # Traverse the strings
    while (pntr1 < len1 and pntr2 < len2):
 
        # Append the smaller of the
        # two current characters
        if (s1[pntr1] < s2[pntr2]):
            ans += s1[pntr1]
            pntr1 += 1
        else:
            ans += s2[pntr2]
            pntr2 += 1
 
    # Append the remaining characters
    # of any of the two strings
    if (pntr1 < len1):
        ans += s1[pntr1:pntr1 + len1]
 
    if (pntr2 < len2):
        ans += s2[pntr2:pntr2 + len2]
 
    # Print the final string
    print (ans)
 
# Driver Code
if __name__ == '__main__':
    S1 = "abdcdtx"
    S2 = "achilp"
 
    # Function Call
    mergeStrings(S1, S2)
 
# This code is contributed by mohit kumar 29.


C#
// C# program for the above approach
using System;
class GFG
{
   
  // Function to find lexicographically
  // smallest string possible by merging
  // two sorted strings
  static void mergeStrings(string s1, string s2)
  {
     
    // Stores length of string s1
    int len1 = s1.Length;
 
    // Stores length of string s2
    int len2 = s2.Length;
 
    // Pointer to beginning
    // of string1 i.e., s1
    int pntr1 = 0;
 
    // Pointer to beginning
    // of string2 i.e., s2
    int pntr2 = 0;
 
    // Stores the final string
    string ans = "";
 
    // Traverse the strings
    while (pntr1 < len1 && pntr2 < len2) {
 
      // Append the smaller of the
      // two current characters
      if (s1[pntr1] < s2[pntr2]) {
        ans += s1[pntr1];
        pntr1++;
      }
 
      else {
        ans += s2[pntr2];
        pntr2++;
      }
    }
 
    // Append the remaining characters
    // of any of the two strings
    if (pntr1 < len1) {
      ans += s1.Substring(pntr1, len1 - pntr1);
    }
    if (pntr2 < len2) {
      ans += s2.Substring(pntr2, len2 - pntr2);
    }
 
    // Print the final string
    Console.WriteLine(ans);
  }
 
  // Driver Code
  public static void Main()
  {
    string S1 = "abdcdtx";
    string S2 = "achilp";
 
    // Function Call
    mergeStrings(S1, S2);
  }
}
 
// This code is contributed by ukasp.


Javascript


输出:
aabcdcdhilptx

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

如果您想与行业专家一起参加直播课程,请参阅Geeks Classes Live