After reading the asked questions about this topic I understand this:
1. The resulting chain mustn't be the longest
2. I assume that the chaining is at the beginning or at the end.
I tested my code with the following input:
CaaT Okinawa Washington
Auckland Kalamazoo zoooZ zooe
Norfolk
The output was:
Washington Norfolk Kalamazoo Okinawa Auckland
Maybe I am missing something about the conditions of validation. Can I get a hint?
package com.codegym.task.task22.task2209;
/*
Make a word chain
*/
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.*;
public class Solution {
public static void main(String[] args) throws IOException {
// ...
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
String fileName = reader.readLine();
reader.close();
BufferedReader reader1 = new BufferedReader(new FileReader(fileName));
ArrayList<String> list = new ArrayList<>();
String data;
while (((data = reader1.readLine()) != null)) {
String[] array = data.trim().split(" ");
list.addAll(Arrays.asList(array));
}
String[] words = list.toArray(new String[0]);
StringBuilder result = getLine(words);
System.out.println(result.toString());
}
public static StringBuilder getLine(String... words) {
StringBuilder builder = new StringBuilder("");
//I add all the words into an ArrayList
if(words.length!=0) {
ArrayList<String> list1 = new ArrayList<>();
Collections.addAll(list1, words);
//here I want to remove all the words that don't have a chained word
for (int i = 0; i < list1.size(); ) {
//I set a counter which is the size of the list
int counter = list1.size();
//I set a second counter which will increment if no match is found
int secondCounter = 0;
//I start with the word at index i and I will compare the first and last letter
//of the word with the first and last letter of the hole list
String firstWord = list1.get(i);
String wordFirstChar = String.valueOf(firstWord.charAt(0));
String wordLastChar = String.valueOf(firstWord.charAt(firstWord.length() - 1));
for (int j = 0; j < list1.size(); j++) {
//the second word, the first letter and the last letter initialization
String secondWord = list1.get(j);
String secondWordFirstChar = String.valueOf(secondWord.charAt(0));
String secondWordLastChar = String.valueOf(secondWord.charAt(secondWord.length() - 1));
//if there is no match, I increment the second counter
if (!wordLastChar.equalsIgnoreCase(secondWordFirstChar) && (!wordFirstChar.equalsIgnoreCase(secondWordLastChar))) {
secondCounter++;
}
}
//I check to see if the first and last letter of the word are equal
//thus eliminating words like "Americana" who don't match other words
//but match it and not incrementing
if (wordFirstChar.equalsIgnoreCase(wordLastChar)) {
secondCounter++;
}
//I check if the initial counter is equal to the second counter
//If it is equal it means that we have iterated trough the hole list and
//didn't find a match. If true we remove the element from the list
//if not, we increment i.
if (counter == secondCounter) {
list1.remove(i);
} else {
i++;
}
}
//I start the builder with the first element of the list.
builder = new StringBuilder(list1.get(0));
//I declare a boolean breacker to exit the outher loop
boolean breacker = false;
for (int i = 0; i < list1.size(); ) {
//I declare the first word as the hole builder and
//initialize the first and last letter
String firstWord = builder.toString();
String wordFirstChar = String.valueOf(firstWord.charAt(0));
String wordLastChar = String.valueOf(firstWord.charAt(firstWord.length() - 1));
//I loop trough the hole loop to find a match of the first letter or the last letter
for (int j = 0; j < list1.size(); j++) {
String secondWord = list1.get(j);
String secondWordFirstChar = String.valueOf(secondWord.charAt(0));
String secondWordLastChar = String.valueOf(secondWord.charAt(secondWord.length() - 1));
//If I find a match on the last letter I append the word to the builder
//remove the element from the list and break
if (wordLastChar.equalsIgnoreCase(secondWordFirstChar)) {
builder.append(" ").append(secondWord);
list1.remove(j);
break;
//If I find a match on the first letter I insert the word to the builder
//at index 0, remove the element from the list and break
} else if (wordFirstChar.equalsIgnoreCase(secondWordLastChar)) {
builder.insert(0, secondWord + " ");
list1.remove(j);
break;
}
//if I loop trough the hole list and don't find a match for the chain
//this means that there is no more word to add
if (j == list1.size() - 1) {
//I set the breacker to true
breacker = true;
}
}
//I breack the outer loop
if (breacker) {
break;
}
}
}
return builder;
}
}