BAEL-5646: String permutation improvements (#12613)
This commit is contained in:
parent
fad5f79b8c
commit
2d5e7da1fa
@ -1,14 +1,20 @@
|
||||
package com.baeldung.algorithms.stringpermutation;
|
||||
|
||||
import java.util.Collection;
|
||||
import java.util.List;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
public class ArrayHelper {
|
||||
public class Helper {
|
||||
|
||||
private ArrayHelper() {
|
||||
private Helper() {
|
||||
}
|
||||
|
||||
static List<Character> toCharacterList(final String string) {
|
||||
return string.chars().mapToObj(s -> ((char) s)).collect(Collectors.toList());
|
||||
}
|
||||
|
||||
static String toString(Collection<Character> collection) {
|
||||
return collection.stream().map(Object::toString).collect(Collectors.joining());
|
||||
}
|
||||
|
||||
}
|
@ -1,20 +1,29 @@
|
||||
package com.baeldung.algorithms.stringpermutation;
|
||||
|
||||
import java.util.Collection;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.stream.Collectors;
|
||||
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 List<String> eagerPermutationWithRepetitions(final String string) {
|
||||
final List<Character> characters = Helper.toCharacterList(string);
|
||||
return CollectionUtils.permutations(characters)
|
||||
.stream()
|
||||
.map(Helper::toString)
|
||||
.collect(Collectors.toList());
|
||||
}
|
||||
|
||||
public PermutationIterator<Character> lazyPermutationWithoutRepetitions(final String string) {
|
||||
final List<Character> characters = ArrayHelper.toCharacterList(string);
|
||||
return new PermutationIterator<>(characters);
|
||||
public List<String> lazyPermutationWithoutRepetitions(final String string) {
|
||||
final List<Character> characters = Helper.toCharacterList(string);
|
||||
final PermutationIterator<Character> permutationIterator = new PermutationIterator<>(characters);
|
||||
final List<String> result = new ArrayList<>();
|
||||
while (permutationIterator.hasNext()) {
|
||||
result.add(Helper.toString(permutationIterator.next()));
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -7,19 +7,21 @@ 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<String> permutationWithoutRepetitions(final String string) {
|
||||
List<Character> chars = Helper.toCharacterList(string);
|
||||
return Generator.permutation(chars)
|
||||
.simple()
|
||||
.stream()
|
||||
.map(Helper::toString)
|
||||
.collect(Collectors.toList());
|
||||
}
|
||||
|
||||
public List<List<Character>> permutationWithRepetitions(final String string) {
|
||||
List<Character> chars = ArrayHelper.toCharacterList(string);
|
||||
public List<String> permutationWithRepetitions(final String string) {
|
||||
List<Character> chars = Helper.toCharacterList(string);
|
||||
return Generator.permutation(chars)
|
||||
.simple(TreatDuplicatesAs.IDENTICAL)
|
||||
.stream()
|
||||
.map(Helper::toString)
|
||||
.collect(Collectors.toList());
|
||||
}
|
||||
|
||||
|
@ -1,18 +1,22 @@
|
||||
package com.baeldung.algorithms.stringpermutation;
|
||||
|
||||
import com.google.common.collect.Collections2;
|
||||
import java.util.Collection;
|
||||
import java.util.List;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
public class StringPermutationsGuava {
|
||||
|
||||
public Collection<List<Character>> permutationWithRepetitions(final String string) {
|
||||
final List<Character> characters = ArrayHelper.toCharacterList(string);
|
||||
return Collections2.permutations(characters);
|
||||
public List<String> permutationWithRepetitions(final String string) {
|
||||
final List<Character> characters = Helper.toCharacterList(string);
|
||||
return Collections2.permutations(characters).stream()
|
||||
.map(Helper::toString)
|
||||
.collect(Collectors.toList());
|
||||
}
|
||||
public List<String> permutationWithoutRepetitions(final String string) {
|
||||
final List<Character> characters = Helper.toCharacterList(string);
|
||||
return Collections2.orderedPermutations(characters).stream()
|
||||
.map(Helper::toString)
|
||||
.collect(Collectors.toList());
|
||||
}
|
||||
public Collection<List<Character>> permutationWithoutRepetitions(final String string) {
|
||||
final List<Character> characters = ArrayHelper.toCharacterList(string);
|
||||
return Collections2.orderedPermutations(characters);
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -0,0 +1,36 @@
|
||||
package com.baeldung.algorithms.stringpermutation;
|
||||
|
||||
import static org.assertj.core.api.AssertionsForClassTypes.assertThat;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
import java.util.stream.Stream;
|
||||
import org.junit.jupiter.params.ParameterizedTest;
|
||||
import org.junit.jupiter.params.provider.Arguments;
|
||||
import org.junit.jupiter.params.provider.MethodSource;
|
||||
|
||||
class HelperUnitTest {
|
||||
|
||||
@ParameterizedTest
|
||||
@MethodSource("stringProvider")
|
||||
void toListTest(String value, List<Character> expected) {
|
||||
final List<Character> actual = Helper.toCharacterList(value);
|
||||
assertThat(expected).isEqualTo(actual);
|
||||
}
|
||||
|
||||
@ParameterizedTest
|
||||
@MethodSource("stringProvider")
|
||||
void toStringTest(String expected, List<Character> value) {
|
||||
final String actual = Helper.toString(value);
|
||||
assertThat(expected).isEqualTo(actual);
|
||||
}
|
||||
|
||||
static Stream<Arguments> stringProvider() {
|
||||
return Stream.of(
|
||||
Arguments.of("hello", Arrays.asList('h', 'e', 'l', 'l', 'o')),
|
||||
Arguments.of("abc", Arrays.asList('a','b','c')),
|
||||
Arguments.of("12345", Arrays.asList('1', '2', '3', '4', '5'))
|
||||
);
|
||||
}
|
||||
|
||||
}
|
@ -2,22 +2,19 @@ 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 {
|
||||
|
||||
@ParameterizedTest
|
||||
@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 List<String> permutations = permutationGenerator.eagerPermutationWithRepetitions(string);
|
||||
final int size = permutations.size();
|
||||
assertThat(permutations)
|
||||
.as("\"%s\" should have %d permutation, but had %d", string, numberOfPermutations, size)
|
||||
@ -30,12 +27,8 @@ class StringPermutationsApacheUnitTest {
|
||||
"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;
|
||||
}
|
||||
final List<String> permutations = permutationGenerator.lazyPermutationWithoutRepetitions(string);
|
||||
int size = permutations.size();
|
||||
assertThat(size)
|
||||
.as("\"%s\" should have %d permutation, but had %d", string, numberOfPermutations, size)
|
||||
.isEqualTo(numberOfPermutations);
|
||||
|
@ -14,7 +14,7 @@ class StringPermutationsCombinatoricsLibUnitTest {
|
||||
"aaaaaa, 720"})
|
||||
void testPermutationsWithRepetitions(String string, int numberOfPermutations) {
|
||||
StringPermutationsCombinatoricsLib permutationGenerator = new StringPermutationsCombinatoricsLib();
|
||||
final List<List<Character>> permutations = permutationGenerator.permutationWithRepetitions(string);
|
||||
final List<String> permutations = permutationGenerator.permutationWithRepetitions(string);
|
||||
final int size = permutations.size();
|
||||
assertThat(permutations)
|
||||
.as("\"%s\" should have %d permutation, but had %d", string, numberOfPermutations, size)
|
||||
@ -27,7 +27,7 @@ class StringPermutationsCombinatoricsLibUnitTest {
|
||||
"aaaaaa, 1"})
|
||||
void testPermutationsWithoutRepetitions(String string, int numberOfPermutations) {
|
||||
StringPermutationsCombinatoricsLib permutationGenerator = new StringPermutationsCombinatoricsLib();
|
||||
final List<List<Character>> permutations = permutationGenerator.permutationWithoutRepetitions(string);
|
||||
final List<String> permutations = permutationGenerator.permutationWithoutRepetitions(string);
|
||||
final int size = permutations.size();
|
||||
assertThat(permutations)
|
||||
.as("\"%s\" should have %d permutation, but had %d", string, numberOfPermutations, size)
|
||||
|
@ -2,7 +2,6 @@ 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;
|
||||
@ -15,7 +14,7 @@ class StringPermutationsGuavaUnitTest {
|
||||
"aaaaaa, 720"})
|
||||
void testPermutationsWithRepetitions(String string, int numberOfPermutations) {
|
||||
StringPermutationsGuava permutationGenerator = new StringPermutationsGuava();
|
||||
final Collection<List<Character>> permutations = permutationGenerator.permutationWithRepetitions(string);
|
||||
final List<String> permutations = permutationGenerator.permutationWithRepetitions(string);
|
||||
final int size = permutations.size();
|
||||
assertThat(permutations)
|
||||
.as("\"%s\" should have %d permutation, but had %d", string, numberOfPermutations, size)
|
||||
@ -28,7 +27,7 @@ class StringPermutationsGuavaUnitTest {
|
||||
"aaaaaa, 1"})
|
||||
void testPermutationsWithoutRepetitions(String string, int numberOfPermutations) {
|
||||
StringPermutationsGuava permutationGenerator = new StringPermutationsGuava();
|
||||
final Collection<List<Character>> permutations = permutationGenerator.permutationWithoutRepetitions(string);
|
||||
final List<String> permutations = permutationGenerator.permutationWithoutRepetitions(string);
|
||||
final int size = permutations.size();
|
||||
assertThat(permutations)
|
||||
.as("\"%s\" should have %d permutation, but had %d", string, numberOfPermutations, size)
|
||||
|
Loading…
x
Reference in New Issue
Block a user