gpt4 book ai didi

java - JAVA 字符数组中的特定元素排列?

转载 作者:塔克拉玛干 更新时间:2023-11-03 04:19:42 25 4
gpt4 key购买 nike

如何列出字符数组中指定的任何字母的所有大写/小写排列?所以,假设我有一个这样的字符数组:['h','e','l','l','o']我想打印出字母“l”的可能组合,以便打印出 [hello,heLlo,heLLo,helLo]。

这是我到目前为止所拥有的(唯一的问题是我可以打印排列,但是我无法在实际单词中打印它们。所以我的代码打印 [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;
}

}

最佳答案

您需要对程序进行少量更改。您的逻辑是完美的,您需要首先找到要在给定单词中更改的 characters 。找到它们后,找到 characterspowerset 来打印所有排列,但这只会打印 rule-char 字符的 permuatation -set 出现在给定单词中。

您需要做的改动很少,首先找到word 的所有indexes,其中包含rule-char-set 的字符。然后找到存储在 ArrayList 中的索引的所有 subsets,然后对于每个子集中的每个元素,使 character 出现在 上>index 到大写字母,这将为您提供所需的所有排列

考虑一个 word = "Hello"rule-char-set="hl" 的例子 那么首先你需要找到 h 的所有索引l 在字符串 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

关于java - JAVA 字符数组中的特定元素排列?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43773952/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com