我正在尝试编写一个函数,该函数将带有小写字母的字符串数组作为输入。函数返回一个具有相同字符串的数组,但每个字符串都重新排列其字母,如果可能的话,它会成为回文,如果没有,则返回-1。
public static Object buildPalin(String[] arrayOfStrings)
{
String[] palindrome = new String[arrayOfStrings.length];
int offset = 0;
int charWithoutReflection = 0;
List<String> whole= Arrays.asList(arrayOfStrings);
String wholeString = String.join("", whole);
for(int i=0; i< arrayOfStrings.length; i++)
{
if (arrayOfStrings[i] != "")
{
int currentCharPosition = wholeString.indexOf(arrayOfStrings[i],
i + 1);
if (currentCharPosition != -1)
{
palindrome[offset] = arrayOfStrings[i];
palindrome[palindrome.length - 1 - offset] =
arrayOfStrings[i];
arrayOfStrings[currentCharPosition] = "";
arrayOfStrings[i] = "";
}
else {
if (charWithoutReflection > 0)
return -1;
palindrome[palindrome.length/2] = arrayOfStrings[i];
charWithoutReflection++;
}
offset++;
}
}
return palindrome;
}
这个函数适用于输入数组是这样的情况 {“a”,“b”,“a”} {“a”,“b”,“c”}等。
但是当我输入如下内容时: {“aba”,“bb”,“cac”}和这样的字符串数组失败。
对此有任何指导或建议是有帮助的。
注意: 这不是课堂作业,而是我在面试中提出的一个问题。 我能够提出上述解决方案,但现在我正在尝试使其适用于角落案例。这将有助于未来的采访。
==============================根据以下Eric的评论更新代码=========
public static Object build(String[] arrayOfStrings) {
String[] palindrome = new String[arrayOfStrings.length];
int offset = 0;
int charWithoutReflection = 0;
List<String> whole= Arrays.asList(arrayOfStrings);
String wholeString = String.join("", whole);
for(int i=0; i< arrayOfStrings.length; i++)
{
if (arrayOfStrings[i] != "") {
int currentCharPosition = wholeString.indexOf(arrayOfStrings[i], i +
1);
if (currentCharPosition != -1)
{
palindrome[offset] = arrayOfStrings[i];
palindrome[palindrome.length - 1 - offset] = arrayOfStrings[i];
arrayOfStrings[currentCharPosition] = "";
arrayOfStrings[i] = "";
}
else {
if (charWithoutReflection > 0)
{
System.out.println("failed case");
return -1;
}
palindrome[palindrome.length/2] = arrayOfStrings[i];
charWithoutReflection++;
}
offset++;
}
}
return palindrome;
}
// code from main function calling above function is:
public static void main(String[] args) {
String a[] = {"abb"};
for ( int j = 0; j < a.length ; j++)
{
String[] tokens = a[j].split("");
System.out.println(Arrays.deepToString((Object[]) build(tokens)));
}
产生错误输出:(对于输入[a,b,b]
[null,b,null]而不是[b,a,b]
答案 0 :(得分:0)
对于每个String
,将其转换为已排序的List
或字符数组。然后逐个字符地查看列表,计算每个字符的出现次数。如果数字(n)是奇数,则将n个出现的字符插入输出String
的中间。如果n是偶数,则将n / 2置于输出String
的开头,将n / 2置于末尾。如果遇到两个以上出现奇数的字符,则必须返回-1。当您到达列表的末尾时,将输出String
放入输出数组中。如果你完全了解,那就返回那个阵列。
public static Object build(String[] arrayOfStrings) {
String[] palindrome = new String[arrayOfStrings.length];
for (int i = 0; i < arrayOfStrings.length; i++) {
String currentString = arrayOfStrings[i];
if (currentString.isEmpty()) {
palindrome[i] = currentString;
} else {
char[] chars = Arrays.copyOf(currentString.toCharArray(),
currentString.length() + 1);
chars[currentString.length()] = Character.MAX_VALUE;
Arrays.parallelSort(chars);
char currentChar = chars[0];
int count = 1;
boolean foundOdd = false;
String newString = "";
for (int j = 1; j < chars.length; j++) {
if (chars[j] == currentChar) {
count++;
} else {
if (foundOdd) {
if (count % 2 == 0) {
newString = even(newString, currentChar, count);
} else {
return -1;
}
} else {
if (count % 2 == 0) {
newString = even(newString, currentChar, count);
} else {
foundOdd = true;
newString = odd(newString, currentChar, count);
}
}
count = 1;
currentChar = chars[j];
}
}
palindrome[i] = newString;
newString = "";
}
}
return palindrome;
}
private static String even(String oldString, char currentChar, int count) {
assert count % 2 == 0;
int halfCount = count / 2;
String occurrences = "";
for (int i = 0; i < halfCount; i++) {
occurrences += currentChar;
}
return occurrences + oldString + occurrences;
}
private static String odd(String oldString, char currentChar, int count) {
assert count % 2 == 1;
int length = oldString.length() / 2;
String occurrences = "";
for (int i = 0; i < count; i++) {
occurrences += currentChar;
}
return oldString.substring(0, length) + occurrences
+ oldString.substring(length);
}
// code from main function calling above function is:
public static void main(String[] args) {
String a[] = { "abb", "aaaabbcccddddeeffgghhii" };
Object result = build(a);
if (result.equals(-1)) {
System.out.println("Failed to find a palindrome for at least one of the input values");
} else {
String[] resultStr = (String[]) result;
for (int i = 0; i < resultStr.length; i++) {
System.out.println(resultStr[i]);
}
}
}
上面的代码太可怕了。一方面,它严重依赖于String
连接。但它确实有效。
答案 1 :(得分:0)
我认为你应该把你的解决方案分成两个单独的案例奇数案例和偶数案例。对于这两种情况都要做一个频率字符的计数数组。
First Case:
1) Check that all chars have even count but one.
2) Create a stringBuffer and append the odd char and decrement its count
3) For each of the remaining characters form your palindrome by adding a char at the front and a char at the end of the string buffer.
Second Case:
1) Check that all chars have even counts.
2) Create a string buffer
3) For each of the remaining characters from your palindrome as case one.