📌  相关文章
📜  反转字符串的最小相邻交换数

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

反转字符串的最小相邻交换数

给定一个字符串 小号任务是最小化反转字符串所需的相邻交换次数。

例子:

方法:这个问题可以通过比较给定的字符串和它的反向字符串并计算形成反向字符串所需的交换次数来解决。请按照以下步骤解决问题:

  • 将字符串s2初始化为原始字符串s的副本。
  • 反向字符串s2
  • 初始化结果 = 0 ,以存储反转字符串所需的相邻交换次数。
  • 对两个字符串都使用两个指针 i 和 j 进行迭代,并通过两个指针ijs2中找到s的每次出现
    • 每次设置result = result + j – i
  • 返回结果作为最终答案。

下面是上述方法的实现:

C++
// C++ program for above approach
#include 
using namespace std;
 
// Function to find minimum adjacent swaps
// Required to reverse the string
int min_swaps(string s)
{
    string s2 = s;
 
    // Reverse a string
    reverse(s2.begin(), s2.end());
 
    int i = 0, j = 0;
    int result = 0;
    int n = s.length();
    while (i < n) {
 
        j = i;
 
        // Iterate till characters
        // of both the strings match
        while (s[j] != s2[i]) {
            j += 1;
        }
 
        // Iterating until i=j
        // result will be j-i
        while (i < j) {
            char temp = s[j];
            s[j] = s[j - 1];
            s[j - 1] = temp;
            j -= 1;
            result += 1;
        }
        i += 1;
    }
    return result;
}
 
// Driver code
int main()
{
    string s = "abc";
 
    cout << min_swaps(s);
    return 0;
}


Java
// Java program for above approach
public class GFG {
     
    static int min_swaps(String s)
    {
        String s2 = "";
 
        // Reverse a string
        char[] cArray = s.toCharArray();
        for (int k = cArray.length - 1; k > -1; k--) {
            s2 += cArray[k];
        }
 
        int i = 0, j = 0;
        int result = 0;
        int n = s.length();
        while (i < n) {
 
            j = i;
 
            // Iterate till characters
            // of both the strings match
            while (s.charAt(j) != s2.charAt(i)) {
                j += 1;
            }
 
            // Iterating until i=j
            // result will be j-i
            while (i < j) {
                char temp = s.charAt(j);
                char[] ch = s.toCharArray();
                ch[j] = ch[j - 1];
                ch[j - 1] = temp;
                s = new String(ch);
                j -= 1;
                result += 1;
            }
            i += 1;
        }
        return result;
    }
 
    // Driver code
    static public void main(String []args)
    {
        String s = "abc";
 
        System.out.println(min_swaps(s));
    }
}
 
// This code is contributed by AnkThon


Python3
# python program for above approach
 
# Function to find minimum adjacent swaps
# Required to reverse the string
def min_swaps(s):
 
    s2 = s.copy()
 
    # Reverse a string
    s2.reverse()
 
    i = 0
    j = 0
    result = 0
    n = len(s)
    while (i < n):
 
        j = i
 
        # Iterate till characters
        # of both the strings match
        while (s[j] != s2[i]):
            j += 1
 
            # Iterating until i=j
            # result will be j-i
        while (i < j):
            temp = s[j]
            s[j] = s[j - 1]
            s[j - 1] = temp
            j -= 1
            result += 1
 
        i += 1
 
    return result
 
# Driver code
if __name__ == "__main__":
 
    s = "abc"
    s = list(s)
    print(min_swaps(s))
 
    # This code is contributed by rakeshsahni


C#
using System;
 
public class GFG {
    static int min_swaps(string s)
    {
        string s2 = String.Empty;
 
        // Reverse a string
        char[] cArray = s.ToCharArray();
        for (int k = cArray.Length - 1; k > -1; k--) {
            s2 += cArray[k];
        }
 
        int i = 0, j = 0;
        int result = 0;
        int n = s.Length;
        while (i < n) {
 
            j = i;
 
            // Iterate till characters
            // of both the strings match
            while (s[j] != s2[i]) {
                j += 1;
            }
 
            // Iterating until i=j
            // result will be j-i
            while (i < j) {
                char temp = s[j];
                char[] ch = s.ToCharArray();
                ch[j] = ch[j - 1];
                ch[j - 1] = temp;
                s = new string(ch);
                j -= 1;
                result += 1;
            }
            i += 1;
        }
        return result;
    }
 
    // Driver code
    static public void Main()
    {
        string s = "abc";
 
        Console.WriteLine(min_swaps(s));
    }
}
 
// This code is contributed by maddler.


Javascript


输出:
3

时间复杂度:O(N 2 ),其中 N 是给定字符串的大小。

辅助空间:O(1)。