BAEL-5646: String permutation examples (#12595)

This commit is contained in:
Eugene Kovko 2022-08-14 20:35:52 +02:00 committed by GitHub
parent 06ac43bc9e
commit e983f3572b
8 changed files with 222 additions and 13 deletions

View File

@ -13,7 +13,11 @@
<version>1.0.0-SNAPSHOT</version> <version>1.0.0-SNAPSHOT</version>
</parent> </parent>
<dependencies> <properties>
<combinatoricslib3.version>3.3.3</combinatoricslib3.version>
<commons-collections4.version>4.4</commons-collections4.version>
</properties>
<dependencies>
<dependency> <dependency>
<groupId>com.google.guava</groupId> <groupId>com.google.guava</groupId>
<artifactId>guava</artifactId> <artifactId>guava</artifactId>
@ -25,6 +29,16 @@
<version>${lombok.version}</version> <version>${lombok.version}</version>
<scope>provided</scope> <scope>provided</scope>
</dependency> </dependency>
</dependencies> <dependency>
<groupId>com.github.dpaukov</groupId>
<artifactId>combinatoricslib3</artifactId>
<version>${combinatoricslib3.version}</version>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-collections4</artifactId>
<version>${commons-collections4.version}</version>
</dependency>
</dependencies>
</project> </project>

View File

@ -0,0 +1,14 @@
package com.baeldung.algorithms.stringpermutation;
import java.util.List;
import java.util.stream.Collectors;
public class ArrayHelper {
private ArrayHelper() {
}
static List<Character> toCharacterList(final String string) {
return string.chars().mapToObj(s -> ((char) s)).collect(Collectors.toList());
}
}

View File

@ -0,0 +1,20 @@
package com.baeldung.algorithms.stringpermutation;
import java.util.Collection;
import java.util.List;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.collections4.iterators.PermutationIterator;
public class StringPermutationsApache {
public Collection<List<Character>> eagerPermutationWithRepetitions(final String string) {
final List<Character> characters = ArrayHelper.toCharacterList(string);
return CollectionUtils.permutations(characters);
}
public PermutationIterator<Character> lazyPermutationWithoutRepetitions(final String string) {
final List<Character> characters = ArrayHelper.toCharacterList(string);
return new PermutationIterator<>(characters);
}
}

View File

@ -0,0 +1,26 @@
package com.baeldung.algorithms.stringpermutation;
import java.util.List;
import java.util.stream.Collectors;
import org.paukov.combinatorics3.Generator;
import org.paukov.combinatorics3.PermutationGenerator.TreatDuplicatesAs;
public class StringPermutationsCombinatoricsLib {
public List<List<Character>> permutationWithoutRepetitions(final String string) {
List<Character> chars = ArrayHelper.toCharacterList(string);
return Generator.permutation(chars)
.simple()
.stream()
.collect(Collectors.toList());
}
public List<List<Character>> permutationWithRepetitions(final String string) {
List<Character> chars = ArrayHelper.toCharacterList(string);
return Generator.permutation(chars)
.simple(TreatDuplicatesAs.IDENTICAL)
.stream()
.collect(Collectors.toList());
}
}

View File

@ -0,0 +1,18 @@
package com.baeldung.algorithms.stringpermutation;
import com.google.common.collect.Collections2;
import java.util.Collection;
import java.util.List;
public class StringPermutationsGuava {
public Collection<List<Character>> permutationWithRepetitions(final String string) {
final List<Character> characters = ArrayHelper.toCharacterList(string);
return Collections2.permutations(characters);
}
public Collection<List<Character>> permutationWithoutRepetitions(final String string) {
final List<Character> characters = ArrayHelper.toCharacterList(string);
return Collections2.orderedPermutations(characters);
}
}

View File

@ -0,0 +1,43 @@
package com.baeldung.algorithms.stringpermutation;
import static org.assertj.core.api.Assertions.assertThat;
import java.util.Collection;
import java.util.List;
import org.apache.commons.collections4.iterators.PermutationIterator;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.CsvSource;
class StringPermutationsApacheUnitTest {
@CsvSource({"abc, 6",
"hello, 120",
"aaaaaa, 720"})
@DisplayName("Apache permutation for ")
void testPermutationsWithRepetitions(String string, int numberOfPermutations) {
StringPermutationsApache permutationGenerator = new StringPermutationsApache();
final Collection<List<Character>> permutations = permutationGenerator.eagerPermutationWithRepetitions(string);
final int size = permutations.size();
assertThat(permutations)
.as("\"%s\" should have %d permutation, but had %d", string, numberOfPermutations, size)
.hasSize(numberOfPermutations);
}
@ParameterizedTest
@CsvSource({"abc, 6",
"hello, 120",
"aaaaaa, 720"})
void testPermutationsWithoutRepetitions(String string, int numberOfPermutations) {
StringPermutationsApache permutationGenerator = new StringPermutationsApache();
final PermutationIterator<Character> permutations = permutationGenerator.lazyPermutationWithoutRepetitions(string);
int size = 0;
while (permutations.hasNext()) {
permutations.next();
++size;
}
assertThat(size)
.as("\"%s\" should have %d permutation, but had %d", string, numberOfPermutations, size)
.isEqualTo(numberOfPermutations);
}
}

View File

@ -0,0 +1,36 @@
package com.baeldung.algorithms.stringpermutation;
import static org.assertj.core.api.Assertions.assertThat;
import java.util.List;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.CsvSource;
class StringPermutationsCombinatoricsLibUnitTest {
@ParameterizedTest
@CsvSource({"abc, 6",
"hello, 120",
"aaaaaa, 720"})
void testPermutationsWithRepetitions(String string, int numberOfPermutations) {
StringPermutationsCombinatoricsLib permutationGenerator = new StringPermutationsCombinatoricsLib();
final List<List<Character>> permutations = permutationGenerator.permutationWithRepetitions(string);
final int size = permutations.size();
assertThat(permutations)
.as("\"%s\" should have %d permutation, but had %d", string, numberOfPermutations, size)
.hasSize(numberOfPermutations);
}
@ParameterizedTest
@CsvSource({"abc, 6",
"hello, 60",
"aaaaaa, 1"})
void testPermutationsWithoutRepetitions(String string, int numberOfPermutations) {
StringPermutationsCombinatoricsLib permutationGenerator = new StringPermutationsCombinatoricsLib();
final List<List<Character>> permutations = permutationGenerator.permutationWithoutRepetitions(string);
final int size = permutations.size();
assertThat(permutations)
.as("\"%s\" should have %d permutation, but had %d", string, numberOfPermutations, size)
.hasSize(numberOfPermutations);
}
}

View File

@ -0,0 +1,38 @@
package com.baeldung.algorithms.stringpermutation;
import static org.assertj.core.api.Assertions.assertThat;
import java.util.Collection;
import java.util.List;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.CsvSource;
class StringPermutationsGuavaUnitTest {
@ParameterizedTest
@CsvSource({"abc, 6",
"hello, 120",
"aaaaaa, 720"})
void testPermutationsWithRepetitions(String string, int numberOfPermutations) {
StringPermutationsGuava permutationGenerator = new StringPermutationsGuava();
final Collection<List<Character>> permutations = permutationGenerator.permutationWithRepetitions(string);
final int size = permutations.size();
assertThat(permutations)
.as("\"%s\" should have %d permutation, but had %d", string, numberOfPermutations, size)
.hasSize(numberOfPermutations);
}
@ParameterizedTest
@CsvSource({"abc, 6",
"hello, 60",
"aaaaaa, 1"})
void testPermutationsWithoutRepetitions(String string, int numberOfPermutations) {
StringPermutationsGuava permutationGenerator = new StringPermutationsGuava();
final Collection<List<Character>> permutations = permutationGenerator.permutationWithoutRepetitions(string);
final int size = permutations.size();
assertThat(permutations)
.as("\"%s\" should have %d permutation, but had %d", string, numberOfPermutations, size)
.hasSize(numberOfPermutations);
}
}