2017-05-04 51 views
7

如何列出字符数组中指定的任何字母的所有大写字母/小写字母排列? 所以说,我有这样的字符阵列:['h','e','l','l','o'] 我想打印出可能的组合,比如说'l'它会打印出[你好,heLlo,heLLo,helLo]。JAVA中的字符数组中的特定元素排列?

这是我到目前为止(唯一的问题是我可以打印排列但是我不能打印它们在实际单词中,所以我的代码打印[ll,ll,ll,LL])上面的例子中

我的代码:。

import java.util.ArrayList; 
import java.util.HashSet; 

public class Main { 

public static void main(String[] args) { 
    //Sample Word 
    String word = "Tomorrow-Today"; 

    //Sample Letters for permutation 
    String rule_char_set = "tw"; 


    ArrayList<Character> test1 = lettersFound(word, rule_char_set); 

    printPermutations(test1); 







} 

public static void printPermutations(ArrayList<Character> arrayList) { 
    char[] chars = new char[arrayList.size()]; 
    int charIterator = 0; 

    for(int i=0; i<arrayList.size(); i++){ 
     chars[i] = arrayList.get(i); 
    } 

    for (int i = 0, n = (int) Math.pow(2, chars.length); i < n; i++) { 
     char[] permutation = new char[chars.length]; 
     for (int j =0; j < chars.length; j++) { 
      permutation[j] = (isBitSet(i, j)) ? Character.toUpperCase(chars[j]) : chars[j]; 
     } 
     System.out.println(permutation); 
    } 
} 

public static boolean isBitSet(int n, int offset) { 
    return (n >> offset & 1) != 0; 
} 

public static ArrayList<Character> lettersFound(String word, String rule_char_set) { 

    //Convert the two parameter strings to two character arrays 
    char[] wordArray = word.toLowerCase().toCharArray(); 
    char[] rule_char_setArray = rule_char_set.toLowerCase().toCharArray(); 

    //ArrayList to hold found characters; 
    ArrayList<Character> found = new ArrayList<Character>(); 

    //Increments the found ArrayList that stores the existent values. 
    int foundCounter = 0; 


    for (int i = 0; i < rule_char_setArray.length; i++) { 
     for (int k = 0; k < wordArray.length; k++) { 
      if (rule_char_setArray[i] == wordArray[k]) { 
       found.add(foundCounter, rule_char_setArray[i]); 
       foundCounter++; 

      } 
     } 
    } 
    //Convert to a HashSet to get rid of duplicates 
    HashSet<Character> uniqueSet = new HashSet<>(found); 

    //Convert back to an ArrayList(to be returned) after filtration of duplicates. 
    ArrayList<Character> filtered = new ArrayList<>(uniqueSet); 

    return filtered; 
} 

} 
+2

由错误编辑?现在我只看到一些JS代码和随机字符。 – Mario

回答

1

SANKET马堪尼的答案是完美的。

我可以提供这个问题的一个更客观的态度。

当你有一个字符串进行修改的输入,以及字符,这应该与修改后的情况下(上或下)来代替。 作为输出,您将拥有所有置换字符串。

我会创建一个包含索引的结构,可能的值与改变:

class Change { 
int index; 
char values[]; 
} 

我们需要让所有可能的组合,所以让包括实地它会告诉哪个字符中目前使用我们的结构,并添加一些方法:

class Change { 
int index; 
char values[]; 
int cur; 

void reset() {cur=0;} 
boolen isMax(){return cur==values.length-1;} 
void next(){cur++;} 
char getValue(){ return values[cur]; } 
} 

我们将这些类的列表或数组,那么,我们将投入到一个单独的类

class Combination { 
Change changes[]; 

    void reset() { for (Change c: changes) c.reset();} 

    boolean next() { 
    for (int i=0; i<changes.length; i++) 
     if (changes[i].isMax()) 
     changes[i].reset(); // next change will be taken in cycle, with "next()" 
     else {changes[i].next(); return true;} 

    return false; // all changes are max 
    } 
} 

所以当你通过输入数据初始化你的“组合”类时,你可以在循环中使用它。

Combination c = new Combination(); 
.... // initialization here 
c.reset(); 
do { 
... // update and print your string 
} while (c.next() ); 

“组合”,用更新的输入字符串值的初始化之后我你:)

6

你需要在你的程序一些改变你的逻辑是完美的,你需要先找到在给定的字要修改的characters后找到它们,找到的characters来打印所有的排列但这只会打印permuatation的字符的rule-char-set哪些出现在给定的字。

您需要做出的一些更改是,首先找到包含rule-char-set的字符的所有indexesword。然后找到存储在ArrayList中的索引的全部subsets,然后对每个子集的每个元素,使该index上的character为大写字母,这会给你所需的所有permutation

考虑到word = "Hello"rule-char-set="hl"然后在这里首先你需要找到的hl各项指标在String word一个例子。

所以这里的索引是0,2,3。储存于ArrayList,然后找到其powerset。然后每个subset,使character目前对indexuppercase信。

Word[] = {'h','e','l','l','o'} 
indexes = 0 , 1 , 2 , 3 , 4 


index[]= { 0 , 2 ,3} //Store the indexes of characters which are to be changed 


BITSET  |  SUBSET  |  word 

000   |   -   |  hello 
001   |  {3}  |  helLo 
010   |  {2}  |  heLlo 
011   |  {2,3}  |  heLLo 
100   |  {0}  |  Hello 
101   |  {0,3}  |  HelLo 
110   |  {0,2}  |  HeLlo 
111   |  {0,2,3}  |  HeLLo 

代码:

import java.util.ArrayList; 
import java.util.HashSet; 

public class Main { 

    public static void main(String[] args) { 
     //Sample Word 
     String word = "Tomorrow-Today"; 

     //Sample Letters for permutation 
     String rule_char_set = "tw"; 


     ArrayList<Integer> test1 = lettersFound(word, rule_char_set); //To store the indexes of the characters 

     printPermutations(word,test1); 

    } 

    public static void printPermutations(String word,ArrayList<Integer> arrayList) { 
     char word_array[]=word.toLowerCase().toCharArray(); 
     int length=word_array.length; 
     int index[]=new int[arrayList.size()]; 

     for(int i=0; i<arrayList.size(); i++){ 
      index[i] = arrayList.get(i); 
     } 

     for (int i = 0, n = (int) Math.pow(2, index.length); i < n; i++) { 
      char[] permutation = new char[length]; 

      System.arraycopy(word_array,0,permutation,0,length);  

      //First copy the original array and change 
      //only those character whose indexes are present in subset 

      for (int j =0; j < index.length; j++) { 
       permutation[index[j]] = (isBitSet(i, j)) ? Character.toUpperCase(permutation[index[j]]) : permutation[index[j]]; 
      } 
      System.out.println(permutation); 
     } 
    } 

    public static boolean isBitSet(int n, int offset) { 
     return (n >> offset & 1) != 0; 
    } 

    public static ArrayList<Integer> lettersFound(String word, String rule_char_set) { 

     //Convert the two parameter strings to two character arrays 
     char[] wordArray = word.toLowerCase().toCharArray(); 
     char[] rule_char_setArray = rule_char_set.toLowerCase().toCharArray(); 

     //ArrayList to hold found characters; 
     ArrayList<Integer> found = new ArrayList<Integer>(); 

     //Increments the found ArrayList that stores the existent values. 
     int foundCounter = 0; 


     for (int i = 0; i < rule_char_setArray.length; i++) { 
      for (int k = 0; k < wordArray.length; k++) { 
       if (rule_char_setArray[i] == wordArray[k]) { 
        found.add(foundCounter, k);  //Store the index of the character that matches 
        foundCounter++; 

       } 
      } 
     } 
     return found; 
    } 

} 

输出:

tomorrow-today 
Tomorrow-today 
tomorrow-Today 
Tomorrow-Today 
tomorroW-today 
TomorroW-today 
tomorroW-Today 
TomorroW-Today 
1

对于置换的情况下离开,我认为递归是在可读性方面最合适的,取考虑到在性能方面可能不是最好的。

我的做法是这样的:

public static void main(String[] args) { 
    generateCombinations("hello", "l", ""); 
} 

public static void generateCombinations(String text, String changingLetters, String current) { 
    if (0 == text.length()) { 
     System.out.println(current); 
     return; 
    } 
    String currentLetter = text.substring(0, 1); 
    if (changingLetters.contains(currentLetter)) { 
     generateCombinations(text.substring(1), changingLetters, current + currentLetter.toUpperCase()); 
    } 
    generateCombinations(text.substring(1), changingLetters, current + currentLetter); 
} 

主执行的输出将是:

heLLo 
heLlo 
helLo 
hello 
相关问题