📌  相关文章
📜  计算最小交换以生成字符串回文

📅  最后修改于: 2021-09-07 02:11:23             🧑  作者: Mango

给定一个字符串s ,任务是找出使字符串s回文所需的最小相邻交换次数。如果不可能,则返回-1
例子:

方法
以下是解决此问题的详细步骤。

  1. 取双指针,其中第一个指针从字符串的左侧开始跟踪,第二个指针从字符串的右侧开始跟踪。
  2. 直到我们找到相同的字符,继续将右指针向左移动一步。
  3. 如果未找到相同的字符,则返回 -1。
  4. 如果找到相同的字符,则将右指针的字符向右交换,直到它不在字符串的正确位置。
  5. 增加左指针并重复步骤 2。

下面是上述方法的实现:

C++
// C++ program to Count
// minimum swap to make
// string palindrome
#include 
using namespace std;
 
// Function to Count minimum swap
int countSwap(string s)
{
    // calculate length of string as n
    int n = s.length();
     
    // counter to count minimum swap
    int count = 0;
 
    // A loop which run till mid of
    // string
    for (int i = 0; i < n / 2; i++) {
        // Left pointer
        int left = i;
 
        // Right pointer
        int right = n - left - 1;
 
        // A loop which run from right
        // pointer towards left pointer
        while (left < right) {
            // if both char same then
            // break the loop.
            // If not, then we have to
            // move right pointer to one
            // position left
            if (s[left] == s[right]) {
                break;
            }
            else {
                right--;
            }
        }
 
        // If both pointers are at same
        // position, it denotes that we
        // don't have sufficient characters
        // to make palindrome string
        if (left == right) {
            return -1;
        }
         
        // else swap and increase the count
        for (int j = right; j < n - left - 1;
                j++) {
            swap(s[j], s[j + 1]);
            count++;
        }
    }
 
    return count;
}
 
// Driver code
int main()
{
    string s = "geeksfgeeks";
 
    // Function calling
    int ans1 = countSwap(s);
        
    reverse(s.begin(),s.end());
    int ans2 = countSwap(s);
              
    cout<


Java
// Java program to Count
// minimum swap to make
// string palindrome
import java.util.*;
 
class GFG {
 
    // Function to Count minimum swap
    static int countSwap(String str)
    {
     
        // Legth of string
        int n = str.length();
         
        // it will convert string to
        // char array
        char s[] = str.toCharArray();
 
        // Counter to count minimum
        // swap
        int count = 0;
 
        // A loop which run in half
        // string from starting
        for (int i = 0; i < n / 2;
                i++) {
 
            // Left pointer
            int left = i;
 
            // Right pointer
            int right = n - left - 1;
 
            // A loop which run from
            // right pointer to left
            // pointer
            while (left < right) {
 
                // if both char same
                // then break the loop
                // if not same then we
                // have to move right
                // pointer to one step
                // left
                if (s[left] == s[right]) {
                    break;
                }
                else {
                    right--;
                }
            }
 
            // it denotes both pointer at
            // same position and we don't
            // have sufficient char to make
            // palindrome string
            if (left == right) {
                return -1;
            }
            else {
                for (int j = right;
                    j < n - left - 1; j++) {
                    char t = s[j];
                    s[j] = s[j + 1];
                    s[j + 1] = t;
                    count++;
                }
            }
        }
 
        return count;
    }
 
    // Driver Code
    public static void main(String[] args)
    {
        String s = "geeksfgeeks";
 
        // Function calling
        int ans1 = countSwap(s);
       
        StringBuilder sb=new StringBuilder(s); 
        sb.reverse(); 
        s = sb.toString(); 
 
        int ans2 = countSwap(s);
        if(ans1 > ans2)
          System.out.println(ans1);
        else
          System.out.println(ans2);
    }
}


Python3
''' Python3 program to Count
    minimum swap to make
    string palindrome'''
 
# Function to Count minimum swap
def CountSwap(s, n):
    s = list(s)
 
    # Counter to count minimum swap
    count = 0
    ans = True
 
    # A loop which run in half string
    # from starting
    for i in range(n // 2):
 
        # Left pointer
        left = i
 
        # Right pointer
        right = n - left - 1
 
        # A loop which run from right pointer
        # to left pointer
        while left < right:
 
            # if both char same then
            # break the loop if not
            # same then we have to move
            # right pointer to one step left
            if s[left] == s[right]:
                break
            else:
                right -= 1
 
        # it denotes both pointer at
        # same position and we don't
        # have sufficient char to make
        # palindrome string
        if left == right:
            ans = False
            break
        else:
            for j in range(right, n - left - 1):
                (s[j], s[j + 1]) = (s[j + 1], s[j])
                count += 1
    if ans:
        return (count)
    else:
        return -1
 
 
# Driver Code
s = 'geeksfgeeks'
 
# Legth of string
n = len(s)
 
# Function calling
ans1 = CountSwap(s, n)
ans2 = CountSwap(s[::-1], n)
print(max(ans1, ans2))


C#
// C# program to Count
// minimum swap to make
// string palindrome
using System;
 
class GFG {
  
    // Function to Count minimum swap
    static int countSwap(String str)
    {
      
        // Length of string
        int n = str.Length;
          
        // it will convert string to
        // char array
        char []s = str.ToCharArray();
  
        // Counter to count minimum
        // swap
        int count = 0;
  
        // A loop which run in half
        // string from starting
        for (int i = 0; i < n / 2;
                i++) {
  
            // Left pointer
            int left = i;
  
            // Right pointer
            int right = n - left - 1;
  
            // A loop which run from
            // right pointer to left
            // pointer
            while (left < right) {
  
                // if both char same
                // then break the loop
                // if not same then we
                // have to move right
                // pointer to one step
                // left
                if (s[left] == s[right]) {
                    break;
                }
                else {
                    right--;
                }
            }
  
            // it denotes both pointer at
            // same position and we don't
            // have sufficient char to make
            // palindrome string
            if (left == right) {
                return -1;
            }
            else {
                for (int j = right;
                    j < n - left - 1; j++) {
                    char t = s[j];
                    s[j] = s[j + 1];
                    s[j + 1] = t;
                    count++;
                }
            }
        }
  
        return count;
    }
  
    // Driver Code
    public static void Main(String[] args)
    {
        String s = "geeksfgeeks";
  
        // Function calling
        int ans1 = countSwap(s);
       
        char[] charArray = s.ToCharArray();
        Array.Reverse( charArray );
        s = new string( charArray );
          
        int ans2 = countSwap(s);
         
        if(ans1 > ans2)
           Console.WriteLine(ans1);
        else
          Console.WriteLine(ans2);
    }
}
 
// This code is contributed by sapnasingh4991


输出:

9


复杂性分析
时间复杂度:由于我们在字符串的长度上运行两个嵌套循环,时间复杂度为O(n 2 )
辅助空间:由于我们没有使用任何额外的空间,因此使用的辅助空间是O(1)

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