Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.ArrayList;
- import java.util.List;
- import java.util.Random;
- import edu.emory.mathcs.backport.java.util.Arrays;
- import edu.emory.mathcs.backport.java.util.Collections;
- public class Test {
- List<String> values = null;
- Random random = new Random();
- public static void main(String[] args) throws Exception {
- new Test().getAllCombinations("abcdefghijrwqtwerqwerwqerwqertwqreqwer", 2).forEach((x) -> {
- System.out.println(x);
- });
- }
- /**
- * The method finds all combinations of the words in a predetermined range.
- *
- * @param text
- * @param size
- * @return List<String>
- */
- public List<String> getAllCombinations(String text, int size) {
- if (text.length() > size) {
- int maxCombination = fact(size);
- values = new ArrayList<>(maxCombination);
- String cutsText = text.substring(0, size);
- List<String> letters = Arrays.asList(cutsText.split(""));
- for (int i = 0; i < maxCombination; i++) {
- getRandomWord(letters);
- }
- } else {
- System.out.println("size is not correct");
- }
- return values;
- }
- private String getRandomWord(List<String> letters) {
- String randomWord = "";
- do {
- randomWord = "";
- Collections.shuffle(letters);
- for (String letter : letters) {
- randomWord = randomWord.concat(letter);
- }
- ;
- if (!values.contains(randomWord))
- break;
- } while (true);
- values.add(randomWord);
- return randomWord;
- }
- public int fact(int num) {
- return (num == 0) ? 1 : num * fact(num - 1);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement