Advertisement
Hey_Donny

JustCount

Jun 5th, 2022
20
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.65 KB | None | 0 0
  1. import java.util.*;
  2. import java.util.stream.Collectors;
  3.  
  4. public class SecondTry {
  5. public static void main(String[] args) {
  6. Scanner scanner = new Scanner(System.in);
  7. String input = scanner.nextLine();
  8. splitString(input);
  9. }
  10.  
  11. public static class result {
  12. int max;
  13. char aChar;
  14.  
  15. public result(int max, char aChar) {
  16. this.max = max;
  17. this.aChar = aChar;
  18. }
  19. }
  20.  
  21. static void splitString(String str) {
  22. StringBuilder alphaLow = new StringBuilder(),
  23. alphaHigh = new StringBuilder(),
  24. special = new StringBuilder();
  25.  
  26. for (int i = 0; i < str.length(); i++) {
  27. if (Character.isAlphabetic(str.charAt(i))) {
  28. if (Character.isLowerCase(str.charAt(i))) {
  29. alphaLow.append(str.charAt(i));
  30. } else if (Character.isUpperCase(str.charAt(i))) {
  31. alphaHigh.append(str.charAt(i));
  32. }
  33. } else
  34. special.append(str.charAt(i));
  35. }
  36. String one = alphaLow.toString();
  37. result resultLow = printMaxOccurringChar(one);
  38. String two = alphaHigh.toString();
  39. result resultHigh = printMaxOccurringChar(two);
  40. String three = special.toString();
  41. result resultSpecial = printMaxOccurringChar(three);
  42.  
  43. result[] results = {resultLow, resultHigh, resultSpecial};
  44.  
  45. List<result> newList = Arrays.stream(results).sorted(Comparator.comparing(result -> result.max))
  46. .collect(Collectors.toList());
  47.  
  48. Collections.reverse(newList);
  49.  
  50. for (result result : newList) {
  51. System.out.printf("%c %d", result.aChar, result.max);
  52. System.out.println();
  53. }
  54. }
  55.  
  56. public static result printMaxOccurringChar(String inputString) {
  57.  
  58. HashMap<Character, Integer> charCountMap = new HashMap<>();
  59.  
  60. char[] charArray = inputString.toCharArray();
  61.  
  62. for (char c : charArray) {
  63. if (charCountMap.containsKey(c)) {
  64. charCountMap.put(c, charCountMap.get(c) + 1);
  65. } else {
  66. charCountMap.put(c, 1);
  67. }
  68. }
  69.  
  70. Set<Map.Entry<Character, Integer>> entrySet = charCountMap.entrySet();
  71.  
  72. int maxCount = 0;
  73.  
  74. char maxChar = 0;
  75.  
  76. for (Map.Entry<Character, Integer> entry : entrySet) {
  77. if (entry.getValue() > maxCount) {
  78. maxCount = entry.getValue();
  79.  
  80. maxChar = entry.getKey();
  81. }
  82. }
  83. return new result(maxCount, maxChar);
  84. }
  85. }
  86.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement