Advertisement
Guest User

Untitled

a guest
Oct 13th, 2015
109
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.45 KB | None | 0 0
  1. import java.util.ArrayList;
  2. import java.util.List;
  3. import java.util.Random;
  4.  
  5. import edu.emory.mathcs.backport.java.util.Arrays;
  6. import edu.emory.mathcs.backport.java.util.Collections;
  7.  
  8. public class Test {
  9. List<String> values = null;
  10. Random random = new Random();
  11.  
  12. public static void main(String[] args) throws Exception {
  13. new Test().getAllCombinations("abcdefghijrwqtwerqwerwqerwqertwqreqwer", 2).forEach((x) -> {
  14. System.out.println(x);
  15. });
  16. }
  17.  
  18. /**
  19. * The method finds all combinations of the words in a predetermined range.
  20. *
  21. * @param text
  22. * @param size
  23. * @return List<String>
  24. */
  25. public List<String> getAllCombinations(String text, int size) {
  26. if (text.length() > size) {
  27. int maxCombination = fact(size);
  28. values = new ArrayList<>(maxCombination);
  29. String cutsText = text.substring(0, size);
  30.  
  31. List<String> letters = Arrays.asList(cutsText.split(""));
  32. for (int i = 0; i < maxCombination; i++) {
  33. getRandomWord(letters);
  34. }
  35. } else {
  36. System.out.println("size is not correct");
  37. }
  38. return values;
  39. }
  40.  
  41. private String getRandomWord(List<String> letters) {
  42. String randomWord = "";
  43.  
  44. do {
  45. randomWord = "";
  46. Collections.shuffle(letters);
  47. for (String letter : letters) {
  48. randomWord = randomWord.concat(letter);
  49. }
  50. ;
  51. if (!values.contains(randomWord))
  52. break;
  53. } while (true);
  54. values.add(randomWord);
  55.  
  56. return randomWord;
  57. }
  58.  
  59. public int fact(int num) {
  60. return (num == 0) ? 1 : num * fact(num - 1);
  61. }
  62.  
  63. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement