Advertisement
Guest User

Untitled

a guest
Mar 30th, 2017
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.17 KB | None | 0 0
  1. import edu.princeton.cs.algs4.*;
  2. import edu.princeton.cs.algs4.*;
  3.  
  4. public class FrequencyCounter
  5. {
  6. public static void main(String[] args)
  7. {
  8. int distinct = 0, words = 0;
  9. int minlen = 4;
  10. BST<String, Integer> st = new BST<String, Integer>();
  11. In in = new In(args[0]);
  12.  
  13. // compute frequency counts
  14. while (!in.isEmpty())
  15. {
  16. String key = in.readString();
  17. if (key.length() < minlen)
  18. continue;
  19. words++;
  20. if (st.contains(key))
  21. {
  22. st.put(key, st.get(key) + 1);
  23.  
  24. }
  25. else
  26. {
  27. st.put(key, 1);
  28. distinct++;
  29.  
  30. }
  31. }
  32.  
  33. // find a key with the highest frequency count
  34. String max = "";
  35. st.put(max, 0);
  36. for (String word : st.keys())
  37. {
  38. if (st.get(word) > st.get(max))
  39. max = word;
  40. }
  41.  
  42. StdOut.println(max + " " + st.get(max));
  43. StdOut.println("distinct = " + distinct);
  44. StdOut.println("words = " + words);
  45. }
  46. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement