反转单词作为回文

时间:2015-03-26 16:23:49

标签: java sorting palindrome

你好我一直试图从这个输入形成回文:但我无法得到输出,有人能帮我正确编写函数吗?非常感谢!。

输入:

String[] text ={"ivcci", "oyotta", "cecarar","bbb","babbbb"};
generatePalindromes();

功能:

public static void generatePalindromes(String[] words) {

}

输出:

civic, -1, racecar, bbb, bbabb

这是我的代码:

    if (s == null) {

        return null;
    }

    Map<Character, Integer> letters = new HashMap<Character, Integer>();

    for (int i = 0; i < s.length(); i++) {
        char c = s.charAt(i);
        if (!letters.containsKey(c)) {
            letters.put(c, 1);
        } else
            letters.put(c, letters.get(c) + 1);
    }

    char[] result = new char[s.length()];
    int i = 0, j = result.length - 1;
    Character middle = null;

    for (Entry<Character, Integer> e : letters.entrySet()) {
        int val = e.getValue();
        char c = e.getKey();
        if (val % 2 != 0) {
            if (middle == null && s.length() % 2 != 0) {
                middle = c;
                val--;
            } else
                return "-1";
        }

        for (int k = 0; k < val / 2; k++) {
            result[i++] = c;
            result[j--] = c;
        }
    }
    if (middle != null)
        result[result.length / 2] = middle;

    System.out.println(result);

    return new String(result);

1 个答案:

答案 0 :(得分:0)

这有帮助吗?它用随机字母生成回文。

它不是我的,我发现它here

/**
 * RecursivePalindromeGenerator.java
 *
 * @author David Roazen
 *
 * A class containing a recursive method palindrome() that returns a
 * randomly-generated palindrome of a specified length.
 *
 * The main() method also demonstrates basic exception handling with a try/catch
 * block.
 */

import java.util.*;

public class RecursivePalindromeGenerator {

    /**
     * palindrome()
     *
     * Returns a random palindrome of the specified length, consisting of all
     * uppercase letters. Throws an IllegalArgumentException if asked to
     * generate a palindrome of a negative length.
     * @param length
     * @return 
     */
    public static String palindrome(int length) {

        // ERROR HANDLING:
        // We cannot generate a palindrome of a negative length,
        // so if length is negative we throw an IllegalArgumentException:
        if (length < 0) {
            throw new IllegalArgumentException("The length must be >= 0!");
        }

        // Otherwise we generate a random letter between 'A' and 'Z':
        char randLetter = (char) (int) (Math.random() * ('Z' - 'A' + 1) + 'A');

        // BASE CASES:
        // A palindrome of length 0 is simply an empty String:
        if (length == 0) {
            return "";
        } // A palindrome of length 1 is a String containing a single letter:
        else if (length == 1) {
            return Character.toString(randLetter);
        }

        // RECURSIVE CASE:
        // A palindrome with "length" characters, where length >= 2, 
        // is a String containing our random letter, followed by
        // a palindrome with length - 2 characters, followed by
        // our random letter again:
        return randLetter + palindrome(length - 2) + randLetter;
    }

    /**
     * The main() program prompts the user for the length of the palindrome to
     * generate, then attempts to generate a palindrome of that length by
     * calling palindrome(). If palindrome() throws an IllegalArgumentException
     * (because len was negative), we catch the exception and print out the
     * error message contained inside of it.
     * @param args
     */
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);

        System.out.print("Enter the desired length of the palindrome: ");
        int len = in.nextInt();

        try {
            System.out.println(palindrome(len));
        } catch (IllegalArgumentException e) {
            System.out.println(e.getMessage());
        }
    }
}