(UVA) Ananagrams - Solution
Link to the problem: https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=24&page=show_problem&problem=92
Every word is kept in a group that is featured by its amount of characters. Words that have the same amount of characters are in the same group. Then, the words that are unique in their groups compose the requested answer.
import java.io.*;
import java.util.*;
class Main {
public HashMap<HashMap<Character, Integer>, ArrayList<String>> map;
public void process() throws NumberFormatException, IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
map = new HashMap<>();
String line = br.readLine();
while (!line.equals("#")) {
String[] words = line.split("\\s");
for (int i = 0; i < words.length; i++) {
HashMap<Character, Integer> property = new HashMap<>();
String wordUpperCase = words[i].toUpperCase();
for (int j = 0; j < wordUpperCase.length(); j++) {
if (!property.containsKey(wordUpperCase.charAt(j))) {
property.put(wordUpperCase.charAt(j), 0);
}
property.put(wordUpperCase.charAt(j), property.get(wordUpperCase.charAt(j))+1);
}
if (!map.containsKey(property)) {
map.put(property, new ArrayList<String>());
}
map.get(property).add(words[i]);
}
line = br.readLine();
}
TreeSet<String> output = new TreeSet<>();
for (ArrayList<String> list : map.values()) {
if (list.size() == 1) {
output.add(list.get(0));
}
}
for (String s : output) {
System.out.println(s);
}
return;
}
public static void main(String[] args) throws NumberFormatException, IOException {
Main m = new Main();
m.process();
System.exit(0);
}
}
Every word is kept in a group that is featured by its amount of characters. Words that have the same amount of characters are in the same group. Then, the words that are unique in their groups compose the requested answer.
import java.io.*;
import java.util.*;
class Main {
public HashMap<HashMap<Character, Integer>, ArrayList<String>> map;
public void process() throws NumberFormatException, IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
map = new HashMap<>();
String line = br.readLine();
while (!line.equals("#")) {
String[] words = line.split("\\s");
for (int i = 0; i < words.length; i++) {
HashMap<Character, Integer> property = new HashMap<>();
String wordUpperCase = words[i].toUpperCase();
for (int j = 0; j < wordUpperCase.length(); j++) {
if (!property.containsKey(wordUpperCase.charAt(j))) {
property.put(wordUpperCase.charAt(j), 0);
}
property.put(wordUpperCase.charAt(j), property.get(wordUpperCase.charAt(j))+1);
}
if (!map.containsKey(property)) {
map.put(property, new ArrayList<String>());
}
map.get(property).add(words[i]);
}
line = br.readLine();
}
TreeSet<String> output = new TreeSet<>();
for (ArrayList<String> list : map.values()) {
if (list.size() == 1) {
output.add(list.get(0));
}
}
for (String s : output) {
System.out.println(s);
}
return;
}
public static void main(String[] args) throws NumberFormatException, IOException {
Main m = new Main();
m.process();
System.exit(0);
}
}
Comments
Post a Comment