📌  相关文章
📜  通过交换相邻的字符对使字符串字典顺序最小且非回文

📅  最后修改于: 2021-09-05 08:42:07             🧑  作者: Mango

鉴于由小写字母的字符串str,任务是通过交换任何一对相邻的字符从字符串任意次数来构建字典序最小的非回文的字符串。
如果给定的字符串无法转换为字典序最小的非回文字符串,则打印“ -1”

例子:

朴素的方法:这个想法是生成字符串的所有可能排列并检查它们是否形成回文。打印其中最小的排列。

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

有效的方法:我们的想法是要检查,如果字典序最小的字符串从给定的字符串可能是回文与否。以下是步骤:

  1. 要获得按字典顺序排列的最小字符串,请对给定字符串进行排序,以按升序排列字符串中的字符。
  2. 现在,如果排序后的字符串是回文,则表示该字符串只有一种字符,不能排列成非回文的字符串。
  3. 如果不是回文,则排序后的字符串是字典序中不是回文的最小字符串。

下面是上述方法的实现:

C++
// C++ program for the above approach
 
#include 
using namespace std;
 
// Function to find the lexographically
// smallest string which is non-palindrome
void smallestNonPalindromic(string& s)
{
 
    // Sort the given string
    sort(s.begin(), s.end());
 
    // Store reverse of sorted string
    string reversestring
        = string(s.rbegin(), s.rend());
 
    // Check if the sorted string is
    // palindromic or not
    if (s != reversestring) {
        cout << s;
    }
    else {
        cout << "-1";
    }
}
 
// Driver Code
int main()
{
    // Given string str
    string str = "asmfjdeovnhekfnj";
 
    // Function Call
    smallestNonPalindromic(str);
    return 0;
}


Java
// Java program for the above approach
import java.util.*;
class GFG{
 
// reverse string
static String reverse(String input)
{
  char[] a = input.toCharArray();
  int l, r = a.length - 1;
  for (l = 0; l < r; l++, r--)
  {
    char temp = a[l];
    a[l] = a[r];
    a[r] = temp;
  }
  return String.valueOf(a);
}
 
// Method to sort a string alphabetically
static String sortString(String inputString)
{
  // convert input string to char array
  char tempArray[] = inputString.toCharArray();
 
  // sort tempArray
  Arrays.sort(tempArray);
 
  // return new sorted string
  return new String(tempArray);
}
   
// Function to find the lexographically
// smallest String which is non-palindrome
static void smallestNonPalindromic(String s)
{
  // Sort the given String
  s = sortString(s);
 
  // Store reverse of sorted String
  String reverseString = reverse(s);
 
  // Check if the sorted String is
  // palindromic or not
  if (s != reverseString)
  {
    System.out.print(s);
  }
   
  else
  {
    System.out.print("-1");
  }
}
 
// Driver Code
public static void main(String[] args)
{
  // Given String str
  String str = "asmfjdeovnhekfnj";
 
  // Function Call
  smallestNonPalindromic(str);
}
}
 
// This code is contributed by gauravrajput1


Python3
# Python3 program for the above approach
 
# Function to find the lexographically
# smallest string which is non-palindrome
def smallestNonPalindromic(s):
 
    # Sort the given string
    s = sorted(s)
 
    # Store reverse of sorted string
    reversestring = s[::-1]
 
    # Check if the sorted string is
    # palindromic or not
    if (s != reversestring):
        print("".join(s))
    else:
        print("-1")
 
# Driver Code
if __name__ == '__main__':
     
    # Given string str
    str = "asmfjdeovnhekfnj"
 
    # Function call
    smallestNonPalindromic(str)
 
# This code is contributed by mohit kumar 29


C#
// C# program for the above approach
using System;
 
class GFG{
 
// Reverse string
static String reverse(String input)
{
    char[] a = input.ToCharArray();
    int l, r = a.Length - 1;
     
    for(l = 0; l < r; l++, r--)
    {
        char temp = a[l];
        a[l] = a[r];
        a[r] = temp;
    }
    return String.Join("", a);
}
 
// Method to sort a string alphabetically
static String sortString(String inputString)
{
     
    // Convert input string to char array
    char []tempArray = inputString.ToCharArray();
     
    // Sort tempArray
    Array.Sort(tempArray);
     
    // Return new sorted string
    return new String(tempArray);
}
 
// Function to find the lexographically
// smallest String which is non-palindrome
static void smallestNonPalindromic(String s)
{
     
    // Sort the given String
    s = sortString(s);
     
    // Store reverse of sorted String
    String reverseString = reverse(s);
     
    // Check if the sorted String is
    // palindromic or not
    if (s != reverseString)
    {
        Console.Write(s);
    }
    else
    {
        Console.Write("-1");
    }
}
 
// Driver Code
public static void Main(String[] args)
{
     
    // Given String str
    String str = "asmfjdeovnhekfnj";
     
    // Function call
    smallestNonPalindromic(str);
}
}
 
// This code is contributed by Amit Katiyar


Javascript


输出:
adeeffhjjkmnnosv

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

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