📌  相关文章
📜  通过交换具有奇数差的相邻数字来最小化给定数字

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

通过交换具有奇数差的相邻数字来最小化给定数字

给定一个长度为N的字符串S代表一个仅由字符'1'、'2' 和 '3' 组成的整数,任务是通过交换相邻字符(如果它们的绝对值)来找到可以从字符串中形成的最小数任何次数的差异都是奇数。

例子:

方法:这个问题可以通过使用贪心方法很容易地解决,使用以下观察:

按照下面提到的步骤来实施观察:

  • 计算字符串中出现的所有 2。
  • 如果它们位于字符串的开头,则保持 1 组不变,即也将它们保持在结果字符串的开头。
  • 在这组 1 之后添加字符串的所有 2。
  • 最后,将剩余的 1 和 3 以与给定字符串中相同的顺序添加到结果字符串中(因为 1 和 3 不能交换)。

下面是上述方法的实现。

C++
// C++ program for above approach
 
#include 
using namespace std;
 
// Function to find minimum number
// corresponding to the given string
// by swapping adjacent numbers if
// their difference is odd
string findMinimumString(string S)
{
    // Finding length of given string
    int N = S.length();
 
    // Declaring variable to store
    // the first occurrence of 3
    int pos = -1;
 
    // Declaring variables to store
    // the count of 1 before the first 3
    // and count of all 2 in given string
    int count1 = 0, count2 = 0;
 
    // Traversing the string and counting the
    // 1s before first occurrence of 3
    // and count of 2s in whole string
    for (int i = 0; i < N; i++) {
        if (pos == -1 && S[i] == '1')
            count1++;
        else if (pos == -1 && S[i] == '3')
            pos = i;
        else if (S[i] == '2')
            count2++;
    }
 
    // Declaring empty string to store answer
    string answer = "";
 
    // Adding all occurrences of 1
    // before first occurrence of 3
    // to the answer
    while (count1--)
        answer += '1';
 
    // Adding all the occurrences of 2
    // into the answer
    while (count2--)
        answer += '2';
 
    // Adding the rest of the occurrences
    // of 1 and 3 to the answer in same order
    // as that in given string
    if (pos != -1) {
        while (pos < N) {
            if (S[pos] != '2')
                answer += S[pos];
            pos++;
        }
    }
 
    // Returning answer string
    return answer;
}
 
// Driver Code
int main()
{
    string S = "213123";
    string minimum_string
        = findMinimumString(S);
    cout << minimum_string;
    return 0;
}


Java
// JAVA program for above approach
import java.util.*;
class GFG
{
   
    // Function to find minimum number
    // corresponding to the given string
    // by swapping adjacent numbers if
    // their difference is odd
    public static String findMinimumString(String S)
    {
       
        // Finding length of given string
        int N = S.length();
 
        // Declaring variable to store
        // the first occurrence of 3
        int pos = -1;
 
        // Declaring variables to store
        // the count of 1 before the first 3
        // and count of all 2 in given string
        int count1 = 0, count2 = 0;
 
        // Traversing the string and counting the
        // 1s before first occurrence of 3
        // and count of 2s in whole string
        for (int i = 0; i < N; i++) {
            if (pos == -1 && S.charAt(i) == '1')
                count1++;
            else if (pos == -1 && S.charAt(i) == '3')
                pos = i;
            else if (S.charAt(i) == '2')
                count2++;
        }
 
        // Declaring empty string to store answer
        String answer = "";
 
        // Adding all occurrences of 1
        // before first occurrence of 3
        // to the answer
        while ((count1--) != 0)
            answer += '1';
 
        // Adding all the occurrences of 2
        // into the answer
        while ((count2--) != 0)
            answer += '2';
 
        // Adding the rest of the occurrences
        // of 1 and 3 to the answer in same order
        // as that in given string
        if (pos != -1) {
            while (pos < N) {
                if (S.charAt(pos) != '2')
                    answer += S.charAt(pos);
                pos++;
            }
        }
 
        // Returning answer string
        return answer;
    }
 
    // Driver Code
    public static void main(String[] args)
    {
        String S = "213123";
        String minimum_string = findMinimumString(S);
        System.out.print(minimum_string);
    }
}
 
// This code is contributed by Taranpreet


Python3
# Function to find minimum number
# corresponding to the given string
# by swapping adjacent numbers if
# their difference is odd
def findMinimumString(S) :
     
    # Finding length of given string
    N = len(S)
 
    # Declaring variable to store
    # the first occurrence of 3
    pos = -1
 
    # Declaring variables to store
    # the count of 1 before the first 3
    # and count of all 2 in given string
    count1 = 0
    count2 = 0
 
    # Traversing the string and counting the
    # 1s before first occurrence of 3
    # and count of 2s in whole string
    for i in range(N) :
        if (pos == -1 and S[i] == '1'):
            count1 += 1
        elif (pos == -1 and S[i] == '3'):
            pos = i
        elif (S[i] == '2') :
            count2 += 1
 
    # Declaring empty string to store answer
    answer = ""
 
    # Adding all occurrences of 1
    # before first occurrence of 3
    # to the answer
    while (count1) :
        answer += '1'
        count1 -= 1
 
    # Adding all the occurrences of 2
    # into the answer
    while (count2):
        answer += '2'
        count2 -= 1
 
    # Adding the rest of the occurrences
    # of 1 and 3 to the answer in same order
    # as that in given string
    if (pos != -1) :
        while (pos < N) :
            if (S[pos] != '2'):
                answer += S[pos]
            pos += 1
         
    # Returning answer string
    return answer
 
# Driver Code
S = "213123"
minimum_string = findMinimumString(S)
print(minimum_string)
 
# This code is contributed by sanjoy_62.


C#
// C# program for above approach
using System;
class GFG
{
 
  // Function to find minimum number
  // corresponding to the given string
  // by swapping adjacent numbers if
  // their difference is odd
  static String findMinimumString(string S)
  {
 
    // Finding length of given string
    int N = S.Length;
 
    // Declaring variable to store
    // the first occurrence of 3
    int pos = -1;
 
    // Declaring variables to store
    // the count of 1 before the first 3
    // and count of all 2 in given string
    int count1 = 0, count2 = 0;
 
    // Traversing the string and counting the
    // 1s before first occurrence of 3
    // and count of 2s in whole string
    for (int i = 0; i < N; i++) {
      if (pos == -1 && S[i] == '1')
        count1++;
      else if (pos == -1 && S[i] == '3')
        pos = i;
      else if (S[i] == '2')
        count2++;
    }
 
    // Declaring empty string to store answer
    string answer = "";
 
    // Adding all occurrences of 1
    // before first occurrence of 3
    // to the answer
    while ((count1--) != 0)
      answer += '1';
 
    // Adding all the occurrences of 2
    // into the answer
    while ((count2--) != 0)
      answer += '2';
 
    // Adding the rest of the occurrences
    // of 1 and 3 to the answer in same order
    // as that in given string
    if (pos != -1) {
      while (pos < N) {
        if (S[pos] != '2')
          answer += S[pos];
        pos++;
      }
    }
 
    // Returning answer string
    return answer;
  }
 
  // Driver Code
  public static void Main()
  {
    string S = "213123";
    string minimum_string = findMinimumString(S);
    Console.Write(minimum_string);
  }
}
 
// This code is contributed by Samim Hossain Mondal.


Javascript



输出
122313

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