Как-то печально все с этой задачей. Код вроде бы что-то похожее на нужный результат выдает, но валидатор категорически не принимает по последнему пункту. Знаю, можно и проще все написать, но хочу именно подобным способом решить.
package com.javarush.task.task22.task2207;
import java.util.HashSet;
import java.util.LinkedList;
import java.util.List;
import java.io.*;
import java.util.Set;
/*
Обращенные слова
*/
public class Solution {
public static List<Pair> result = new LinkedList<>();
public static void main(String[] args) throws IOException {
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
String name = reader.readLine();
FileReader fr = new FileReader(name);
StringBuilder str = new StringBuilder();
while (fr.ready()) {
str.append((char)fr.read());
}
String check = str.toString();
String[] arr = check.split(" ");
StringBuilder arrReverse = new StringBuilder(check);
arrReverse = arrReverse.reverse();
String reversedStr = arrReverse.toString();
String[] strChecking = reversedStr.split(" ");
int x = strChecking.length - 1;
for (String s : arr) {
x--;
for (int i = strChecking.length - 1; i >= 0; i--) {
Pair pair = new Pair();
if (x != i) {
StringBuilder sb = new StringBuilder(strChecking[i]);
String res = sb.reverse().toString();
if (s.equals(strChecking[i])) {
pair.first = s;
pair.second = res;
result.add(pair);
}
}
}
}
Set<Pair> hs= new HashSet<>(result);
result.clear();
result.addAll(hs);
}
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;
}
}
}
package com.javarush.task.task22.task2207;
import java.util.HashSet;
import java.util.LinkedList;
import java.util.List;
import java.io.*;
import java.util.Set;
/*
Обращенные слова
*/
public class Solution {
public static List<Pair> result = new LinkedList<>();
public static void main(String[] args) throws IOException {
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
String name = reader.readLine();
FileReader fr = new FileReader(name);
StringBuilder str = new StringBuilder();
while (fr.ready()) {
str.append((char)fr.read());
}
String check = str.toString();
String[] arr = check.split(" ");
StringBuilder arrReverse = new StringBuilder(check);
arrReverse = arrReverse.reverse();
String reversedStr = arrReverse.toString();
String[] strChecking = reversedStr.split(" ");
int x = strChecking.length - 1;
for (String s : arr) {
x--;
for (int i = strChecking.length - 1; i >= 0; i--) {
Pair pair = new Pair();
if (x != i) {
StringBuilder sb = new StringBuilder(strChecking[i]);
String res = sb.reverse().toString();
if (s.equals(strChecking[i])) {
pair.first = s;
pair.second = res;
result.add(pair);
}
}
}
}
Set<Pair> hs= new HashSet<>(result);
result.clear();
result.addAll(hs);
//System.out.println(result);
}
//C:\Users\warder\Desktop\text.txt
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;
}
}
}