Advertisement
Guest User

Untitled

a guest
May 25th, 2018
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.52 KB | None | 0 0
  1. package com.company;
  2.  
  3. import java.io.IOException;
  4. import java.util.*;
  5. import java.util.stream.Collectors;
  6.  
  7. public class SolutionWordSequencePermutations {
  8.  
  9.     public static void main(String[] args) throws IOException {
  10.         List<String> input = Arrays.asList("ojhg", "yuio", "qwerty", "bbb", "zalupa", "anus", "pidor", "soy");
  11.         List<String> usableWords = findUsableWords(input);
  12.         System.out.println("input: " + input);
  13.         System.out.println("usableWords: " + usableWords);
  14.         Set<List<String>> result = new HashSet<>();
  15.         for (List<String> variant : PermutationsGenerator.generatePermutations(usableWords)) {
  16.             int length = checkIfSequence(variant);
  17.             if (length > 1) {
  18.                 System.out.println(variant.stream().limit(length).collect(Collectors.toList()));
  19.                 result.add(variant.stream().limit(length).collect(Collectors.toList()));
  20.             }
  21.         }
  22.         System.out.println("============COMBINATIONS============");
  23.         result.forEach(System.out::println);
  24.         System.out.println("====================================");
  25.     }
  26.  
  27.     static class PermutationsGenerator {
  28.  
  29.         private static List<List<String>> generatePermutations(List<String> input) {
  30.             List<List<String>> result = new ArrayList<>();
  31.             if (input.size() == 1) {
  32.                 result.add(input);
  33.             } else {
  34.                 for (int i = 0; i < input.size(); i++) {
  35.                     for (List<String> tmpPart : generatePermutations(getListCopyExcept(input, i))) {
  36.                         List<String> tmp = new ArrayList<>();
  37.                         tmp.add(input.get(i));
  38.                         tmp.addAll(tmpPart);
  39.                         result.add(tmp);
  40.                     }
  41.                 }
  42.             }
  43.             return result;
  44.         }
  45.  
  46.         private static List<String> getListCopyExcept(List<String> strings, int except) {
  47.             List<String> result = new ArrayList<>();
  48.             for (int i = 0; i < strings.size(); i++) {
  49.                 if (i != except) {
  50.                     result.add(strings.get(i));
  51.                 }
  52.             }
  53.             return result;
  54.         }
  55.  
  56.     }
  57.  
  58.     private static List<String> findUsableWords(List<String> input) {
  59.         Set<String> result = new HashSet<>();
  60.         for (int i = 0; i < input.size(); i++) {
  61.             String firstLetter = input.get(i).substring(0, 1);
  62.             String lastLetter = input.get(i).substring(input.get(i).length() - 1);
  63.             for (int j = 0; j < input.size(); j++) {
  64.                 if (i != j) {
  65.                     if (input.get(j).endsWith(firstLetter)) {
  66.                         result.add(input.get(i));
  67.                         break;
  68.                     } else if (input.get(j).startsWith(lastLetter)) {
  69.                         result.add(input.get(i));
  70.                         break;
  71.                     }
  72.                 }
  73.             }
  74.         }
  75.         return new ArrayList<>(result);
  76.     }
  77.  
  78.     private static int checkIfSequence(List<String> input) {
  79.         String valueToCompareWith = null;
  80.         int length = 0;
  81.         for (String word : input) {
  82.             if (valueToCompareWith != null) {
  83.                 if (valueToCompareWith.equals(word.substring(0, 1))) {
  84.                     length++;
  85.                 } else {
  86.                     return length;
  87.                 }
  88.             }
  89.             valueToCompareWith = word.substring(word.length() - 1);
  90.         }
  91.         return length;
  92.     }
  93.  
  94. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement