Advertisement
Guest User

Untitled

a guest
Apr 23rd, 2019
112
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.08 KB | None | 0 0
  1. import java.util.ArrayList;
  2. import java.util.Scanner;
  3. import java.util.Collections;
  4. import java.util.List;
  5.  
  6.  
  7. public class testSort {
  8.  
  9. public static void main(String[] args) {
  10. // TODO Auto-generated method stub
  11. Scanner stdin = new Scanner(System.in);
  12. ArrayList<String> list = new ArrayList<String>();
  13. while (stdin.hasNext()) {
  14.  
  15. int length = 0;
  16. int top = 0;
  17. int freq = 0;
  18. String key = "fail";
  19. try {
  20. length = Integer.parseInt(args[0]);
  21.  
  22. }catch(Exception e){
  23. length = 5;
  24. }
  25. String[] possibleWords =
  26. stdin.next().replaceAll("(^[^A-Za-z]+)|([^A-Za-z]+$)", "").split("-");
  27. for(int i = 0; i < possibleWords.length; i++) {
  28. list.add(possibleWords[i]);
  29. }
  30. list.sort(null);
  31. for (int i = 0; i < list.size(); i++) {
  32. freq = Collections.frequency(list, list.get(i));
  33. if(freq > top && list.get(i).length() > length) {
  34. top = freq;
  35. key = list.get(i);
  36. //System.out.printf("index %d",i);
  37. }
  38.  
  39. }
  40.  
  41. System.out.printf("%s %d%n", key, top);
  42. }
  43. //System.out.println(list);
  44. }
  45. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement