2017-05-04 60 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 
相關問題