I think it should pass the tests.
sout is not commented anymore and its still not working.
package com.codegym.task.task22.task2207;
import java.io.*;
import java.nio.charset.StandardCharsets;
import java.util.*;
/*
Inverted words
*/
public class Solution {
public static List<Pair> result = new LinkedList<>();
public static void main(String[] args) throws IOException {
Scanner scanner = new Scanner(System.in);
String fileName = scanner.nextLine();
List<String> strings = new ArrayList<>();
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(new FileInputStream(fileName), StandardCharsets.UTF_8));
while (bufferedReader.ready()) {
String[] parts = bufferedReader.readLine().split(" ");
strings.addAll(Arrays.asList(parts));
}
bufferedReader.close();
for (int i = 0; i < strings.size() - 1; i++) {
StringBuilder reversed = new StringBuilder(strings.get(i));
reversed.reverse();
for (int j = i + 1; j < strings.size(); j++) {
if (strings.get(j).equals(reversed.toString())) {
Pair pair = new Pair();
pair.first = strings.get(i);
pair.second = reversed.toString();
if (!result.contains(pair))
result.add(pair);
}
}
}
// for (Pair pair : result) {
// System.out.println(pair);
// }
}
public static class Pair {
String first;
String second;
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Pair pair = (Pair) o;
if (first != null ? !first.equals(pair.first) : pair.first != null) return false;
return second != null ? second.equals(pair.second) : pair.second == null;
}
@Override
public int hashCode() {
int result = first != null ? first.hashCode() : 0;
result = 31 * result + (second != null ? second.hashCode() : 0);
return result;
}
@Override
public String toString() {
return first == null && second == null ? "" :
first == null ? second :
second == null ? first :
first.compareTo(second) < 0 ? first + " " + second : second + " " + first;
}
}
}