Доброго времени суток, коллеги.
Тестил на различных вариантах - все всегда возвращается верно.
Логично, что последний не проходит из-за предпоследнего. А вот что не так там - вопрос.
Не пойму что не так - кто может подсказать?
package com.javarush.task.task22.task2209;
import java.io.*;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.*;
/*
Составить цепочку слов
*/
public class Solution {
public static void main(String[] args) throws IOException {
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(System.in));
String fileName = bufferedReader.readLine();
bufferedReader.close();
List<String> lines = Files.readAllLines(Paths.get(fileName), StandardCharsets.UTF_8);
StringBuilder result = getLine(lines.toArray(new String[0]));
System.out.println(result.toString());
// permute(lines.get(0).split("\\s"));
}
public static StringBuilder getLine(String... words) throws IOException { //C:\Users\Andrey\Desktop\a.txt
StringBuilder stringBuilder = new StringBuilder();
if (words == null || words.length == 0) {
stringBuilder.append("");
return stringBuilder;
}
String[] help = words[0].split("\\s");
List<String> strings = new ArrayList<>(Arrays.asList(help));
ArrayList<String> wrongWords = getWrongWords(strings);
for (String s : wrongWords) {
strings.remove(s);
}
permute(strings.toArray(new String[0]));
Scanner s = new Scanner(new File("C:\\Users\\Andrey\\Desktop\\b.txt"));
String s1 = s.nextLine();
String[] correct = s1.split("\\s");
// String[] correct = permute(newArray);
// for (String s : correct) stringBuilder.append(s).append(" ");
// for (String s : wrongWords) stringBuilder.append(s).append(" ");
// String[] help = words[0].split("\\s");
// List<String> strings = new ArrayList<>(Arrays.asList(help));
//
// ArrayList<String> wrongWords = getWrongWords(strings);
// for (String s : wrongWords) {
// strings.remove(s);
// }
//
// boolean flag = false;
// while (!flag) {
// int count = 0;
// for (int i = 0; i < strings.size() - 1; i++) {
// if (strings.get(i).substring(strings.get(i).length() - 1).toLowerCase().equals(strings.get(i + 1).substring(0, 1).toLowerCase())) {
// count++;
// } else {
// count = 0;
// Collections.shuffle(strings);
// break;
// }
// }
// if (count == strings.size() - 1) {
// flag = true;
// }
// }
//
for (String line : correct) {
stringBuilder.append(line).append(" ");
}
for (String line : wrongWords) {
stringBuilder.append(line).append(" ");
}
stringBuilder.delete(stringBuilder.length() - 1, stringBuilder.length());
return stringBuilder;
}
public static void permute(String[] arr) throws IOException {
permuteHelper(arr, 0);
}
private static void permuteHelper(String[] arr, int index) throws IOException {
if(index >= arr.length - 1){ //If we are at the last element - nothing left to permute
int count = 0;
for (int i = 0; i < arr.length - 1; i++) {
if (arr[i].substring(arr[i].length() - 1).toLowerCase().equals(arr[i + 1].substring(0, 1).toLowerCase())) {
count++;
} else {
count = 0;
break;
}
}
if (count == arr.length - 1) {
BufferedWriter outputWriter = null;
outputWriter = new BufferedWriter(new FileWriter("C:\\Users\\Andrey\\Desktop\\b.txt"));
for (int i = 0; i < arr.length; i++) {
outputWriter.write(arr[i]+" ");
}
outputWriter.flush();
outputWriter.close();
return;
}
}
for (int i = index; i < arr.length; i++){ //For each index in the sub array arr[index...end]
//Swap the elements at indices index and i
String t = arr[index];
arr[index] = arr[i];
arr[i] = t;
//Recurse on the sub array arr[index+1...end]
permuteHelper(arr, index+1);
//Swap the elements back
t = arr[index];
arr[index] = arr[i];
arr[i] = t;
}
}
public static ArrayList<String> getWrongWords(List<String> strings) {
ArrayList<String> wrong = new ArrayList<>();
for (int i = 0; i < strings.size(); i++) {
String currentWord = strings.get(i);
int count = 0;
for (String string : strings) {
if ((currentWord.substring(0, 1).toLowerCase().equals(string.substring(string.length() - 1).toLowerCase()) ||
currentWord.substring(currentWord.length() - 1).toLowerCase().equals(string.substring(0, 1).toLowerCase()))
&& Character.isLetter(currentWord.toCharArray()[0])) {
count++;
}
}
if (count == 0) {
wrong.add(currentWord);
}
}
return wrong;
}
}