原文:

给你一个长度为 n 的字符串 s(只有小写字母)。打印它必须获得的字符串的每个字符的位置,这样它将形成一个回文字符串。

示例:

input  : c b b a a
output : 3 1 5 2 4
to make string palindrome 'c' must be at position 3,
'b' at 1 and 5, 'a' at 2 and 4.
input : a b c
output : not possible
any permutation of string cannot form palindrome. 

其思想是创建一个向量数组(或动态大小数组),存储每个字符的所有位置。在存储位置之后,我们检查奇数字符的计数是否超过 1。如果是,我们返回“不可能”。否则,我们首先打印数组的前半部分位置,然后打印奇数字符的一个位置(如果存在),最后打印后半部分位置。

c

// cpp program to print original
// positions of characters in a
// string after rearranging and
// forming a palindrome
#include 
using namespace std;
// maximum number of characters
const int max = 256;
void printpalindromepos(string &str)
{
    // insert all positions of every
    // character in the given string.
    vector pos[max];
    int n = str.length();
    for (int i = 0; i < n; i  )
        pos[str[i]].push_back(i 1);
    /* find the number of odd elements.
       takes o(n) */
    int oddcount = 0;
    char oddchar;
    for (int i=0; i 1)
        cout << "no palindrome";
    /* print positions in first half
       of palindrome */
    for (int i=0; i 0)
    {
        int last = pos[oddchar].size() - 1;
        cout << pos[oddchar][last] << " ";
        pos[oddchar].pop_back();
    }
    /* print positions in second half
       of palindrome */
    for (int i=max-1; i>=0; i--)
    {
        int count = pos[i].size();
        for (int j=count/2; j

java 语言(一种计算机语言,尤用于创建网站)

// java program to print original
// positions of characters in a
// string after rearranging and
// forming a palindrome
import java.util.*;
class gfg
{
// maximum number of characters
static int max = 256;
static void printpalindromepos(string str)
{
    // insert all positions of every
    // character in the given string.
    vector []pos = new vector[max];
    for (int i = 0; i < max; i  )
        pos[i] = new vector();
    int n = str.length();
    for (int i = 0; i < n; i  )
        pos[str.charat(i)].add(i   1);
    /* find the number of odd elements.
    takes o(n) */
    int oddcount = 0;
    char oddchar = 0;
    for (int i = 0; i < max; i  )
    {
        if (pos[i].size() % 2 != 0)
        {
            oddcount  ;
            oddchar = (char) i;
        }
    }
    /* a palindrome cannot contain more than 1
    odd characters */
    if (oddcount > 1)
        system.out.print("no palindrome");
    /* print positions in first half
    of palindrome */
    for (int i = 0; i < max; i  )
    {
        int mid = pos[i].size() / 2;
        for (int j = 0; j < mid; j  )
            system.out.print(pos[i].get(j)   " ");
    }
    // consider one instance odd character
    if (oddcount > 0)
    {
        int last = pos[oddchar].size() - 1;
        system.out.print(pos[oddchar].get(last)   " ");
        pos[oddchar].remove(pos[oddchar].size() - 1);
    }
    /* print positions in second half
    of palindrome */
    for (int i = max - 1; i >= 0; i--)
    {
        int count = pos[i].size();
        for (int j = count / 2; j < count; j  )
            system.out.print(pos[i].get(j)   " ");
    }
}
// driver code
public static void main(string[] args)
{
    string s = "geeksgk";
    printpalindromepos(s);
}
}
// this code is contributed by 29ajaykumar

c

// c# program to print original
// positions of characters in a
// string after rearranging and
// forming a palindrome
using system;
using system.collections.generic;
class gfg
{
// maximum number of characters
static int max = 256;
static void printpalindromepos(string str)
{
    // insert all positions of every
    // character in the given string.
    list []pos = new list[max];
    for (int i = 0; i < max; i  )
        pos[i] = new list();
    int n = str.length;
    for (int i = 0; i < n; i  )
        pos[str[i]].add(i   1);
    /* find the number of odd elements.
    takes o(n) */
    int oddcount = 0;
    char oddchar = (char)0;
    for (int i = 0; i < max; i  )
    {
        if (pos[i].count % 2 != 0)
        {
            oddcount  ;
            oddchar = (char) i;
        }
    }
    /* a palindrome cannot contain more than 1
    odd characters */
    if (oddcount > 1)
        console.write("no palindrome");
    /* print positions in first half
    of palindrome */
    for (int i = 0; i < max; i  )
    {
        int mid = pos[i].count / 2;
        for (int j = 0; j < mid; j  )
            console.write(pos[i][j]   " ");
    }
    // consider one instance odd character
    if (oddcount > 0)
    {
        int last = pos[oddchar].count - 1;
        console.write(pos[oddchar][last]   " ");
        pos[oddchar].removeat(pos[oddchar].count - 1);
    }
    /* print positions in second half
    of palindrome */
    for (int i = max - 1; i >= 0; i--)
    {
        int count = pos[i].count;
        for (int j = count / 2; j < count; j  )
            console.write(pos[i][j]   " ");
    }
}
// driver code
public static void main(string[] args)
{
    string s = "geeksgk";
    printpalindromepos(s);
}
}
// this code is contributed by 29ajaykumar

java 描述语言


output: 

2 1 4 5 7 6 3 

时间复杂度 : o ( n )