[ABAB,BABA,ABBA,AABB,BBAA,BAAB]
以下のような要素で構成HashSetのから隣接する文字が重複している要素を削除するコードであります私はAABBとして入力を与えるときにハッシュセットの値を取得するために使用されました
public static Set<String> crunchifyPermutation(String str) {
Set<String> crunchifyResult = new HashSet<String>();
if (str == null) {
return null;
} else if (str.length() == 0) {
crunchifyResult.add("");
return crunchifyResult;
}
char firstChar = str.charAt(0);
String rem = str.substring(1);
Set<String> words = crunchifyPermutation(rem);
for (String newString : words) {
for (int i = 0; i <= newString.length(); i++) {
crunchifyResult.add(crunchifyCharAdd(newString, firstChar, i));
}
}
return crunchifyResult;
}
public static String crunchifyCharAdd(String str, char c, int j) {
String first = str.substring(0, j);
String last = str.substring(j);
return first + c + last;
}
を使用したソリューションであるとの問題は何ですか?それは動作しませんか?任意のエラー? – svarog
このコードは[ABAB、BABA、ABBA、AABB、BBAA、BAAB]を返しますが、隣接する重複文字を含まない2つの要素[ABAB、BABA]のみを返すように変更する必要があります。 –