java - 查找元音始终位于末尾的 Anagrams

标签 java algorithm recursion

我正在尝试这个问题:

使用递归编写一个函数来显示用户输入的字符串的所有字谜词,使其所有元音都位于每个字谜词的末尾。 (例如:递归 => Rcrsneuio、cRsnroieu 等)对其进行优化。

从这个网站: http://erwnerve.tripod.com/prog/recursion/magic.htm

这就是我所做的:

public static void permute(char[] pre,char[] suff) {
    if (isEmpty(suff)) {
        //result is a set of string. toString() method will return String representation of the array.
        result.add(toString(moveVowelstoEnd(pre)));
        return;
    }
    int sufflen = getLength(suff); //gets the length of the array
    for(int i =0;i<sufflen;i++) {
        char[] tempPre = pre.clone();
        char[] tempSuf = suff.clone();
        int nextindex = getNextIndex(pre); //find the next empty spot in the prefix array
        tempPre[nextindex] = tempSuf[i];
        tempSuf = removeElement(i,tempSuf); //removes the element at i and shifts array to the left
        permute(tempPre,tempSuf);
    }

}

public static char[] moveVowelstoEnd(char[] input) {
    int c = 0;
    for(int i =0;i<input.length;i++) {
        if(c>=input.length)
            break;
        char ch = input[i];
        if (vowels.contains(ch+"")) {
            c++;            
            int j = i;
            for(;j<input.length-1;j++)
                input[j] = input[j+1];
            input[j]=ch;
            i--;
        }
    }
    return input;
}

问题的最后一部分是“优化它”。我不知道如何优化这个。有人可以帮忙吗?

最佳答案

将所有元音分组为 v

将所有辅音分组为 w

对于每对字谜,连接结果

关于java - 查找元音始终位于末尾的 Anagrams,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16306965/

相关文章:

java - 我如何在 Java 中处理 1000 位数字?

java - 如何在java中获取没有命名空间的XML节点名称?

algorithm - 在单峰数组中找到第 k 个元素

java - Java中的巴比伦算法

algorithm - 没有比赛顺序的 Elo 评分系统

c++ - 对这个递归示例有点困惑

java - 从任何 java 应用程序调用自己的 eclipse 插件方法

java - 在我的形状计算器 Java 程序中使用带有 Switch/Case 语句的循环

c++ - 在递归中使用引用会导致内存泄漏?

c# - 遍历一个对象的所有后代