From 64142311b005f3281a2ff0f1e00f67e1ae1397e9 Mon Sep 17 00:00:00 2001 From: Richard Kojedzinszky Date: Tue, 30 Sep 2014 14:06:15 +0200 Subject: [PATCH] uwc: java implementation --- uwc/uwc.java | 78 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 78 insertions(+) create mode 100644 uwc/uwc.java diff --git a/uwc/uwc.java b/uwc/uwc.java new file mode 100644 index 0000000..e93f51d --- /dev/null +++ b/uwc/uwc.java @@ -0,0 +1,78 @@ +package uwc; + +import java.io.BufferedReader; +import java.io.BufferedWriter; +import java.io.IOException; +import java.io.InputStreamReader; +import java.io.OutputStreamWriter; +import java.util.ArrayList; +import java.util.Collections; +import java.util.Comparator; +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import java.util.regex.Pattern; +import java.util.regex.Matcher; + +public class uwc { + + + /** + * @param args + * @throws IOException + */ + public static void main(String[] args) throws IOException { + // TODO Auto-generated method stub + final HashMap hash = new HashMap(); + + InputStreamReader isr = new InputStreamReader(System.in); + BufferedReader br = new BufferedReader(isr); + + String line; + Pattern p = Pattern.compile("[a-z']+", Pattern.CASE_INSENSITIVE); + + while ((line = br.readLine()) != null) { + Matcher m = p.matcher(line); + while (m.find()) { + final String w = m.group(); + + Integer v = hash.get(w); + if (v == null) { + v = 1; + } else { + v++; + } + hash.put(w, v); + } + } + + ArrayList words = new ArrayList(); + + for (Map.Entry e : hash.entrySet()) { + words.add(e.getKey()); + } + + Collections.sort(words, new Comparator() { + @Override + public int compare(String o1, String o2) { + // TODO Auto-generated method stub + int diff = hash.get(o1) - hash.get(o2); + if (diff != 0) { + return diff; + } + + return o1.compareTo(o2); + } + }); + + OutputStreamWriter osr = new OutputStreamWriter(System.out); + BufferedWriter bw = new BufferedWriter(osr); + + for (String s : words) { + bw.write(s + " " + hash.get(s).toString() + "\n"); + } + + bw.flush(); + } + +} -- 2.1.4