Почему не принимает? Те тесты которые я написал проходят😡
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));
Scanner sc = new Scanner(new FileReader(console.readLine()));
console.close();
List<String> cities = new ArrayList<>();
while (sc.hasNext()) {
cities.add(sc.next());
}
StringBuilder result = getLine(cities.toArray(new String[0]));
System.out.println(result.toString());
sc.close();
}
public static StringBuilder getLine(String... words) {
if (words.length == 0) {
return new StringBuilder();
}
StringBuilder builder = new StringBuilder();
builder.append(words[0]);
List<String> strings = new ArrayList<>(Arrays.asList(words));
strings.remove(0);
int p = strings.size();
int counter = 0;
while (!strings.isEmpty()) {
if (strings.get(0).startsWith(builder.substring(builder.length() - 1).toUpperCase())) {
builder.append(" ").append(strings.remove(0));
} else if (strings.get(0).endsWith(builder.substring(0, 1).toUpperCase())) {
String str = strings.remove(0);
builder.append(strings.remove(0 ) + " ", 0, str.length());
} else {
strings.add(strings.remove(0));
counter++;
}
if (counter == p) {
while (!strings.isEmpty()) {
builder.append(" ").append(strings.remove(0));
}
}
}
return builder;
}
}