📌  相关文章
📜  两个字符串之间所需的最小交换使一个字符串严格大于另一个

📅  最后修改于: 2021-09-06 05:32:48             🧑  作者: Mango

给定两个长度分别为MN 的字符串AB ,任务是找到使字符串A 在字典序上大于字符串B所需的两个字符的最小交换。

例子:

做法:可以观察到,如果M ≤ N且所有字符都相同,包括两个字符串,那么就不可能使字符串A严格大于字符串B 。否则,字符串A可以通过将两个不同的字符在做得比字符串B严格地 最多两次移动中两个字符串的0索引

请按照以下步骤解决问题:

  1. 首先,检查字符串A的第一个字符是否大于字符串B的第一个字符,然后打印0
  2. 否则,检查是否B[0] > A[0]则需要1 次交换,因此将A[0]B[0]交换并打印1。
  3. 否则,检查两个字符串的所有字符是否相同,并且M ≤ N则不可能,因此打印-1
  4. 否则,检查是否存在位于任何字符比A [0]B中的字符是大于B [0],然后打印1小。
  5. 否则,检查是否存在存在于任何字符小于A [0]B中的任何字符,其大于B [0]然后打印2。
  6. 否则,如果以上条件都不满足,则返回0

下面是上述方法的实现:

C++
// C++ program for the above approach
#include 
using namespace std;
 
// Function to find the minimum
// number of steps to make A > B
int minSteps(string A, string B, int M, int N)
{
 
    if (A[0] > B[0])
        return 0;
 
    if (B[0] > A[0]) {
        return 1;
    }
 
    // If all character are same and M <= N
    if (M <= N && A[0] == B[0]
        && count(A.begin(), A.end(), A[0]) == M
        && count(B.begin(), B.end(), B[0]) == N)
        return -1;
 
    // If there lies any character
    // in B which is greater than B[0]
    for (int i = 1; i < N; i++) {
 
        if (B[i] > B[0])
            return 1;
    }
 
    // If there lies any character
    // in A which is smaller than A[0]
    for (int i = 1; i < M; i++) {
 
        if (A[i] < A[0])
            return 1;
    }
 
    // If there lies a character which
    // is in A and greater than A[0]
    for (int i = 1; i < M; i++) {
 
        if (A[i] > A[0]) {
 
            swap(A[i], B[0]);
            swap(A[0], B[0]);
            return 2;
        }
    }
 
    // If there lies a character which
    // is in B and less than B[0]
    for (int i = 1; i < N; i++) {
 
        if (B[i] < B[0]) {
 
            swap(A[0], B[i]);
            swap(A[0], B[0]);
            return 2;
        }
    }
 
    // Otherwise
    return 0;
}
 
// Driver Code
int main()
{
    string A = "adsfd";
    string B = "dffff";
 
    int M = A.length();
    int N = B.length();
 
    cout << minSteps(A, B, M, N);
 
    return 0;
}


Java
// Java program for above approach
import java.util.*;
import java.lang.*;
class GFG
{
 
  // Function to find the minimum
  // number of steps to make A > B
  static int minSteps(StringBuilder A,
                      StringBuilder B,
                      int M, int N)
  {
 
    if (A.charAt(0) > B.charAt(0))
      return 0;
 
    if (B.charAt(0) > A.charAt(0))
    {
      return 1;
    }
 
    // If all character are same and M <= N
    if (M <= N && A.charAt(0) == B.charAt(0)
        && count(A, A.charAt(0)) == M
        && count(B, B.charAt(0)) == N)
      return -1;
 
    // If there lies any character
    // in B which is greater than B[0]
    for (int i = 1; i < N; i++)
    {
 
      if (B.charAt(i) > B.charAt(0))
        return 1;
    }
 
    // If there lies any character
    // in A which is smaller than A[0]
    for (int i = 1; i < M; i++)
    {
 
      if (A.charAt(i) < A.charAt(0))
        return 1;
    }
 
    // If there lies a character which
    // is in A and greater than A[0]
    for (int i = 1; i < M; i++)
    {
      if (A.charAt(i) > A.charAt(0))
      {
        swap(A, i, B, 0);
        swap(A, 0, B, 0);
        return 2;
      }
    }
 
    // If there lies a character which
    // is in B and less than B[0]
    for (int i = 1; i < N; i++)
    {
      if (B.charAt(i) < B.charAt(0))
      {
        swap(A, 0, B, i);
        swap(A, 0, B, 0);
        return 2;
      }
    }
 
    // Otherwise
    return 0;
  }
 
  static int count(StringBuilder a,
                   char c)
  {
    int count = 0;
    for(int i = 0; i < a.length(); i++)
      if(a.charAt(i) == c)
        count++; 
    return count;  
  }
 
  static void swap(StringBuilder s1,
                   int index1,
                   StringBuilder s2,
                   int index2)
  {
    char c = s1.charAt(index1);
    s1.setCharAt(index1,s2.charAt(index2));
    s2.setCharAt(index2,c);
 
  }
  // Driver function
  public static void main (String[] args)
  {
    StringBuilder A = new StringBuilder("adsfd");
    StringBuilder B = new StringBuilder("dffff");
    int M = A.length();
    int N = B.length();
    System.out.println(minSteps(A, B, M, N));
  }
}
 
// This code is contributed by offbeat.


Python3
# Python3 program for the above approach
 
# Function to find the minimum
# number of steps to make A > B
def minSteps(A, B, M, N):
     
    if (A[0] > B[0]):
        return 0
 
    if (B[0] > A[0]):
        return 1
 
    # If all character are same and M <= N
    if (M <= N and A[0] == B[0] and
           A.count(A[0]) == M and
           B.count(B[0]) == N):
        return -1
 
    # If there lies any character
    # in B which is greater than B[0]
    for i in range(1, N):
        if (B[i] > B[0]):
            return 1
 
    # If there lies any character
    # in A which is smaller than A[0]
    for i in range(1, M):
        if (A[i] < A[0]):
            return 1
 
    # If there lies a character which
    # is in A and greater than A[0]
    for i in range(1, M):
        if (A[i] > A[0]):
            A[0], B[i] = B[i], A[0]
            A[0], B[0] = B[0], A[0]
            return 2
 
    # If there lies a character which
    # is in B and less than B[0]
    for i in range(1, N):
        if (B[i] < B[0]):
            A[0], B[i] = B[i], A[0]
            A[0], B[0] = B[0], A[0]
            return 2
 
    # Otherwise
    return 0
 
# Driver Code
if __name__ == '__main__':
     
    A = "adsfd"
    B = "dffff"
 
    M = len(A)
    N = len(B)
 
    print(minSteps(A, B, M, N))
 
# This code is contributed by mohit kumar 29


C#
// C# program for above approach
using System;
using System.Text;
 
public class GFG
{
 
  // Function to find the minimum
  // number of steps to make A > B
  static int minSteps(StringBuilder A, StringBuilder B,  int M, int N)
  {
    if (A[0] > B[0])
      return 0;
 
    if (B[0] > A[0])
    {
      return 1;
    }
 
    // If all character are same and M <= N
    if (M <= N && A[0] == B[0]
        && count(A, A[0]) == M
        && count(B, B[0]) == N)
      return -1;
 
    // If there lies any character
    // in B which is greater than B[0]
    for (int i = 1; i < N; i++)
    {
 
      if (B[i] > B[0])
        return 1;
    }
 
    // If there lies any character
    // in A which is smaller than A[0]
    for (int i = 1; i < M; i++)
    {
 
      if (A[i] < A[0])
        return 1;
    }
 
    // If there lies a character which
    // is in A and greater than A[0]
    for (int i = 1; i < M; i++)
    {
      if (A[i] > A[0])
      {
        swap(A, i, B, 0);
        swap(A, 0, B, 0);
        return 2;
      }
    }
 
    // If there lies a character which
    // is in B and less than B[0]
    for (int i = 1; i < N; i++)
    {
      if (B[i] < B[0])
      {
        swap(A, 0, B, i);
        swap(A, 0, B, 0);
        return 2;
      }
    }
 
    // Otherwise
    return 0;
 
  }
 
  static int count(StringBuilder a,
                   char c)
  {
    int count = 0;
    for(int i = 0; i < a.Length; i++)
      if(a[i] == c)
        count++; 
    return count;  
  }
 
  static void swap(StringBuilder s1,
                   int index1,
                   StringBuilder s2,
                   int index2)
  {
 
    char c = s1[index1];
    s1[index1] = s2[index2];
    s2[index2] = c;
  }
 
  // Driver function
  static public void Main ()
  {
    StringBuilder A = new StringBuilder("adsfd");
    StringBuilder B = new StringBuilder("dffff");
    int M=A.Length;
    int N=B.Length;
    Console.WriteLine(minSteps(A, B, M, N));
  }
}
 
// This code is contributed by avanitrachhadiya2155


Javascript


输出:
1

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

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