原文:

给定str,任务是打印给定字符串的字典最小的 的最后一个字符。 如果不存在这样的排列,请打印 -1。 示例

输入str = "deepqvu"

输出v

说明:字符串"deepqvu"是字典顺序的非回文最小排列。

因此,最后一个字符是v

输入str = "zyxaaabb"

输出z

说明:字符串"zyxaaabb"是按字典顺序的非回文最小排列。

因此,最后一个字符是z

朴素的方法:解决该问题的最简单方法是,并针对每个排列检查它是否是回文集。 在所有获得的非回文排列中,按字典顺序打印最小的排列的最后一个字符。 请按照以下步骤操作:

  1. 使用函数检查回文的字符串的所有后续排列。

  2. 如果存在非回文的任何排列,则打印其最后一个字符。

  3. 否则,打印-1

下面是上述方法的实现:

c

// c   program for the above approach
#include 
using namespace std;
// function to check whether a string
// s is a palindrome or not
bool ispalin(string s, int n)
{
    // traverse the string
    for (int i = 0; i < n; i  ) {
        // if unequal character
        if (s[i] != s[n - i - 1]) {
            return false;
        }
    }
    return true;
}
// function to find the smallest
// non-palindromic lexicographic
// permutation of string s
void lexicographicsmalleststring(
    string s, int n)
{
    // base case
    if (n == 1) {
        cout << "-1";
    }
    // sort the given string
    sort(s.begin(), s.end());
    int flag = 0;
    // if the formed string is
    // non palindromic
    if (ispalin(s, n) == false)
        flag = 1;
    if (!flag) {
        // check for all permutations
        while (next_permutation(s.begin(),
                                s.end())) {
            // check palindromic
            if (ispalin(s, n) == false) {
                flag = 1;
                break;
            }
        }
    }
    // if non palindromic string found
    // print its last character
    if (flag == 1) {
        int lastchar = s.size() - 1;
        cout << s[lastchar] << ' ';
    }
    // otherwise, print "-1"
    else {
        cout << "-1";
    }
}
// driver code
int main()
{
    // given string str
    string str = "deepqvu";
    // length of the string
    int n = str.length();
    // function call
    lexicographicsmalleststring(str, n);
    return 0;
}

java

// java program for the 
// above approach
import java.util.*;
class gfg{
// function to check whether 
// a string s is a palindrome 
// or not
static boolean ispalin(string s, 
                       int n)
{
  // traverse the string
  for (int i = 0; i < n; i  ) 
  {
    // if unequal character
    if (s.charat(i) != 
        s.charat(n - i - 1)) 
    {
      return false;
    }
  }
  return true;
}
static boolean next_permutation(char[] p) 
{
  for (int a = p.length - 2; 
           a >= 0; --a)
    if (p[a] < p[a   1])
      for (int b = p.length - 1;; --b)
        if (p[b] > p[a]) 
        {
          char t = p[a];
          p[a] = p[b];
          p[b] = t;
          for (  a, b = p.length - 1; 
                 a < b;   a, --b) 
          {
            t = p[a];
            p[a] = p[b];
            p[b] = t;
          }
          return true;
        }
  return false;
}
//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 smallest
// non-palindromic lexicographic
// permutation of string s
static void lexicographicsmalleststring(string s, 
                                        int n)
{
  // base case
  if (n == 1) 
  {
    system.out.print("-1");
  }
  // sort the given string
  s =  sortstring(s);
  int flag = 0;
  // if the formed string is
  // non palindromic
  if (ispalin(s, n) == false)
    flag = 1;
  if (flag != 0) 
  {
    // check for all permutations
    while (next_permutation(s.tochararray())) 
    {
      // check palindromic
      if (ispalin(s, n) == false) 
      {
        flag = 1;
        break;
      }
    }
  }
  // if non palindromic string found
  // print its last character
  if (flag == 1) 
  {
    int lastchar = s.length() - 1;
    system.out.print(s.charat(lastchar)   " ");
  }
  // otherwise, print "-1"
  else
  {
    system.out.print("-1");
  }
}
// driver code
public static void main(string[] args)
{
  // given string str
  string str = "deepqvu";
  // length of the string
  int n = str.length();
  // function call
  lexicographicsmalleststring(str, n);
}
}
// this code is contributed by rajput-ji

c

// c# program for the 
// above approach
using system;
class gfg{
// function to check whether 
// a string s is a palindrome 
// or not
static bool ispalin(string s, 
                    int n)
{
  // traverse the string
  for (int i = 0; i < n; i  ) 
  {
    // if unequal character
    if (s[i] != 
        s[n - i - 1]) 
    {
      return false;
    }
  }
  return true;
}
static bool next_permutation(char[] p) 
{
  for (int a = p.length - 2; 
           a >= 0; --a)
    if (p[a] < p[a   1])
      for (int b = p.length - 1;; --b)
        if (p[b] > p[a]) 
        {
          char t = p[a];
          p[a] = p[b];
          p[b] = t;
          for (  a, b = p.length - 1; 
                 a < b;   a, --b) 
          {
            t = p[a];
            p[a] = p[b];
            p[b] = t;
          }
          return true;
        }
  return false;
}
//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 smallest
// non-palindromic lexicographic
// permutation of string s
static void lexicographicsmalleststring(string s, 
                                        int n)
{
  // base case
  if (n == 1) 
  {
    console.write("-1");
  }
  // sort the given string
  s =  sortstring(s);
  int flag = 0;
  // if the formed string is
  // non palindromic
  if (ispalin(s, n) == false)
    flag = 1;
  if (flag != 0) 
  {
    // check for all permutations
    while (next_permutation(s.tochararray())) 
    {
      // check palindromic
      if (ispalin(s, n) == false) 
      {
        flag = 1;
        break;
      }
    }
  }
  // if non palindromic string found
  // print its last character
  if (flag == 1) 
  {
    int lastchar = s.length - 1;
    console.write(s[lastchar]   " ");
  }
  // otherwise, print "-1"
  else
  {
    console.write("-1");
  }
}
// driver code
public static void main(string[] args)
{
  // given string str
  string str = "deepqvu";
  // length of the string
  int n = str.length;
  // function call
  lexicographicsmalleststring(str, n);
}
}
// this code is contributed by amit katiyar

输出

v 

时间复杂度o(n * n!)

辅助空间o(1)

有效方法:为了优化上述方法,其思想是存储给定字符串str的每个字符的。 如果所有字符都相同,则打印 -1。 否则,打印给定字符串str的最大字符。

下面是上述方法的实现:

c

// c   program for the above approach
#include 
using namespace std;
// function to find the smallest non
// palindromic lexicographic string
void lexicographicsmalleststring(
    string s, int n)
{
    // stores the frequency of each
    // character of the string s
    map m;
    // traverse the string
    for (char ch : s) {
        m[ch]  ;
    }
    // if there is only one element
    if (m.size() == 1) {
        cout << "-1";
    }
    // otherwise
    else {
        auto it = m.rbegin();
        cout << it->first;
    }
}
// driver code
int main()
{
    // given string str
    string str = "deepqvu";
    // length of the string
    int n = str.length();
    // function call
    lexicographicsmalleststring(str, n);
    return 0;
}

python3

# python3 program for the above approach
# function to find the smallest non
# palindromic lexicographic string
def lexicographicsmalleststring(s, n):
    # stores the frequency of each
    # character of the s
    m = {}
    # traverse the string
    for ch in s:
        m[ch] = m.get(ch, 0)   1
    # if there is only one element
    if len(m) == 1:
        print("-1")
    # otherwise
    else:
        x = list(m.keys())[-2]
        print(x)
# driver code
if __name__ == '__main__':
    # given str
    str = "deepqvu"
    # length of the string
    n = len(str)
    # function call
    lexicographicsmalleststring(str, n)
# this code is contributed by mohit kumar 29

输出

v

时间复杂度o(n)

辅助空间o(26)