What does mean "if possible" in the 7th requirement ?
Should we delete all word without any correspondence with the first or last letter of the others words ?
package com.codegym.task.task22.task2209;
/*
Make a word chain
*/
/* Précédente solution
public class Solution {
public static void main(String[] args) {
// ...
String words = "";
try {
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
BufferedReader reader = new BufferedReader(new FileReader(in.readLine()));
//BufferedReader reader = new BufferedReader(new FileReader("C:\\Users\\rivie\\OneDrive\\Documents\\Informatique\\CodeGym\\FichierstxtExo\\Q2N09L11E05\\fichier2.txt"));
while(reader.ready()) {
words += reader.readLine();
}
in.close();
reader.close();
} catch (IOException ioE) {
}
StringBuilder result = getLine(words.split(" "));
System.out.println(result.toString());
}
public static StringBuilder getLine(String... words) {
List <String> wordsList = new ArrayList<>(Arrays.asList(words));
//Seek the first
for (String wordUse : words) {
boolean hasPrevious = false;
char wordUseFirstLetter = Character.toLowerCase(wordUse.charAt(0));
for (String wordToFind : words) {
if (wordUse.equals(wordToFind)) continue;
char wordToFindLastLetter = Character.toLowerCase(wordToFind.charAt(wordToFind.length()-1));
if (wordUseFirstLetter == wordToFindLastLetter) hasPrevious = true;
}
if (!hasPrevious) {
String s = wordUse;
wordsList.remove(wordUse);
wordsList.add(0,s);
}
}
int i = 0;
while (i < wordsList.size()) {
String wordUse = wordsList.get(i);
char wordUseLastLetter = Character.toLowerCase(wordUse.charAt(wordUse.length()-1));
for (int j = i + 1; j < wordsList.size(); j++) {
String wordToFind = wordsList.get(j);
char wordToFindFirstLetter = Character.toLowerCase(wordToFind.charAt(0));
if (wordUseLastLetter == wordToFindFirstLetter) {
String s = wordToFind;
wordsList.remove(wordToFind);
wordsList.add(i+1,s);
break;
}
}
i++;
}
String word = "";
for (String w : wordsList) {
word += w + " ";
}
StringBuilder sb = new StringBuilder(word.trim());
return sb;
}
}
*/
import java.io.*;
import java.util.*;
public class Solution {
public static void main(String[] args) {
// ...
String words = "";
try {
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
BufferedReader reader = new BufferedReader(new FileReader(in.readLine()));
//BufferedReader reader = new BufferedReader(new FileReader("C:\\Users\\rivie\\OneDrive\\Documents\\Informatique\\CodeGym\\FichierstxtExo\\Q2N09L11E05\\fichier2.txt"));
while(reader.ready()) {
words += reader.readLine() + " ";
}
in.close();
reader.close();
} catch (IOException ioE) {
}
//System.out.println(words);
StringBuilder result = getLine(words.split("\\W+"));
System.out.println(result.toString());
}
public static StringBuilder getLine(String... words) {
if (words.length == 0) return new StringBuilder();
if (words.length == 1) return new StringBuilder(words[1]);
List <String> wordsList = new ArrayList<>(Arrays.asList(words));
seekFirst(wordsList, words);
sortWords(wordsList);
String word = "";
for (String w : wordsList) {
word += w + " ";
}
StringBuilder sb = new StringBuilder(word.trim());
return sb;
}
public static void seekFirst(List<String> wordsList, String... words) {
int countWordsWithoutPrevious = 0;
for (String wordUse : words) {
boolean hasPrevious = false;
char wordUseFirstLetter = Character.toLowerCase(wordUse.charAt(0));
for (String wordToFind : words) {
if (wordUse.equals(wordToFind)) continue;
char wordToFindLastLetter = Character.toLowerCase(wordToFind.charAt(wordToFind.length()-1));
if (wordUseFirstLetter == wordToFindLastLetter) hasPrevious = true;
}
if (!hasPrevious) {
String s = wordUse;
wordsList.remove(wordUse);
wordsList.add(0,s);
countWordsWithoutPrevious++;
}
}
}
public static void sortWords(List<String> wordsList) {
int i = 0;
while (i < wordsList.size()) {
String wordUse = wordsList.get(i);
char wordUseLastLetter = Character.toLowerCase(wordUse.charAt(wordUse.length()-1));
for (int j = 0; j < wordsList.size(); j++) {
String wordToFind = wordsList.get(j);
if (wordUse.equals(wordToFind)) continue;
char wordToFindFirstLetter = Character.toLowerCase(wordToFind.charAt(0));
if (wordUseLastLetter == wordToFindFirstLetter) {
String s = wordToFind;
wordsList.remove(wordToFind);
int id = wordsList.indexOf(wordUse);
wordsList.add(id+1,s);
break;
}
}
i++;
}
boolean sorted = true;
for (int j = 1; j < wordsList.size(); j++) {
char previousWordLastLetter = Character.toLowerCase(wordsList.get(j-1).charAt(wordsList.get(j-1).length()-1));
char wordFirstLetter = Character.toLowerCase(wordsList.get(j).charAt(0));
if (previousWordLastLetter != wordFirstLetter) {
sorted = false;
break;
}
if (j == wordsList.size() - 1) continue;
char nextWordFirstLetter = Character.toLowerCase(wordsList.get(j+1).charAt(0));
char wordLastLetter = Character.toLowerCase(wordsList.get(j).charAt(wordsList.get(j).length()-1));
if (nextWordFirstLetter != wordLastLetter) {
sorted = false;
break;
}
}
if (!sorted) sortWords(wordsList);
}
}