Не проходит по последнему пункту!
package com.javarush.task.task22.task2209;
import java.io.*;
import java.util.*;
/*
Составить цепочку слов
*/
public class Solution {
public static void main(String[] args) throws IOException {
BufferedReader console = new BufferedReader(new InputStreamReader(System.in));
BufferedReader fileReader = new BufferedReader(new FileReader(console.readLine()));
console.close();
StringBuilder stringBuilder = new StringBuilder();
while (fileReader.ready()){
stringBuilder.append(fileReader.readLine());
stringBuilder.append(" ");
}
fileReader.close();
String[] strings = stringBuilder.toString().trim().split(" ");
StringBuilder result = getLine(strings);
System.out.println(result.toString());
}
public static StringBuilder getLine(String... words) {
StringBuilder stringBuilder = new StringBuilder();
if(words.length > 0) {
Map<String, Boolean> useTheWords = fillMapAtArray(words);
String firstWordInArray = searchAWord(words, PositionInArray.FIRST);
String lastWordInArray = searchAWord(words, PositionInArray.LAST);
stringBuilder.append(firstWordInArray);
char lastChar = getCharPositionInWord(firstWordInArray, PositionInArray.LAST);
useTheWords.put(firstWordInArray, true);
boolean lastWordIsSearching = false;
int tryToSearch = countWordsIsNotUse(useTheWords) + 3;
while (allWordsIsNotUse(useTheWords) & !lastWordIsSearching) {
for (String word : words) {
if (!useTheWords.get(word)) {
if (getCharPositionInWord(word, PositionInArray.FIRST) == lastChar) {
stringBuilder.append(" ");
stringBuilder.append(word);
lastChar = getCharPositionInWord(word, PositionInArray.LAST);
useTheWords.put(word, true);
if(word.equals(lastWordInArray)){
lastWordIsSearching = true;
break;
}
}
}else if(tryToSearch == 1){
lastWordIsSearching = true;
}
}
tryToSearch--;
}
tryToSearch = countWordsIsNotUse(useTheWords) + 3;
while (allWordsIsNotUse(useTheWords) & tryToSearch > 0){
String[] currentStringWords = stringBuilder.toString().split(" ");
for(int i = currentStringWords.length - 1; i >= 0; i--){
char firstChar = getCharPositionInWord(currentStringWords[i], PositionInArray.FIRST);
for (String word : words) {
if (!useTheWords.get(word)) {
if (getCharPositionInWord(word, PositionInArray.LAST) == firstChar) {
int indexWord = stringBuilder.indexOf(currentStringWords[i]);
stringBuilder.insert(indexWord, word + " ");
useTheWords.put(word, true);
}else if(tryToSearch == 1){
stringBuilder.append(" ");
stringBuilder.append(word);
useTheWords.put(word, true);
}
}
}
tryToSearch--;
}
}
}
return stringBuilder;
}
public static Map<String, Boolean> fillMapAtArray(String[] array){
Map<String, Boolean> map = new HashMap<>();
for(String element : array){
map.put(element, false);
}
return map;
}
public static char getCharPositionInWord(String word, PositionInArray position){
char[] charsInWord = word.toUpperCase(Locale.ROOT).toCharArray();
if(PositionInArray.FIRST == position) return charsInWord[0];
else return charsInWord[charsInWord.length - 1];
}
public static boolean allWordsIsNotUse(Map<String, Boolean> usesWords){
for(Map.Entry<String, Boolean> element : usesWords.entrySet()){
if(!element.getValue()) return true;
}
return false;
}
public static int countWordsIsNotUse(Map<String, Boolean> usesWords){
int countWords = 0;
for(Map.Entry<String, Boolean> element : usesWords.entrySet()){
if(!element.getValue()) countWords++;
}
return countWords;
}
public static String searchAWord(String[] words, PositionInArray positionInArray){
List<Character> firstChar = new ArrayList<>();
List<Character> lastChar = new ArrayList<>();
for(String word: words){
firstChar.add(getCharPositionInWord(word, PositionInArray.FIRST));
lastChar.add(getCharPositionInWord(word, PositionInArray.LAST));
}
for(int i = 0; i < firstChar.size(); i++){
for(int j = 0; j < lastChar.size(); j++){
if(firstChar.get(i).charValue() == lastChar.get(j).charValue()){
firstChar.remove(firstChar.get(i));
lastChar.remove(lastChar.get(j));
i--;
break;
}
}
}
if(firstChar.size() > 0 && (PositionInArray.FIRST == positionInArray)){
for(String word : words) {
if(getCharPositionInWord(word, PositionInArray.FIRST) == firstChar.get(0)){
return word;
}
}
}
if(lastChar.size() > 0) {
for (String word : words) {
if (getCharPositionInWord(word, PositionInArray.LAST) == lastChar.get(0)){
return word;
}
}
}
return words[0];
}
}