Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import edu.princeton.cs.algs4.*;
- import edu.princeton.cs.algs4.*;
- public class FrequencyCounter
- {
- public static void main(String[] args)
- {
- int distinct = 0, words = 0;
- int minlen = 4;
- BST<String, Integer> st = new BST<String, Integer>();
- In in = new In(args[0]);
- // compute frequency counts
- while (!in.isEmpty())
- {
- String key = in.readString();
- if (key.length() < minlen)
- continue;
- words++;
- if (st.contains(key))
- {
- st.put(key, st.get(key) + 1);
- }
- else
- {
- st.put(key, 1);
- distinct++;
- }
- }
- // find a key with the highest frequency count
- String max = "";
- st.put(max, 0);
- for (String word : st.keys())
- {
- if (st.get(word) > st.get(max))
- max = word;
- }
- StdOut.println(max + " " + st.get(max));
- StdOut.println("distinct = " + distinct);
- StdOut.println("words = " + words);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement