sunfresh

JustCount

Jan 18th, 2023
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.71 KB | None | 0 0
  1. Given a string of lowercase characters (a - z), uppercase characters (A - Z) and special symbols ("!", "-", " ", "#", etc.), output the lowercase, uppercase and symbol characters that occurs the most times throughout the string. If two characters occur equally as much in a given category, output the one with lower ASCII value.
  2.  
  3. Input
  4. Read from the standard input
  5.  
  6. There is a single line of input - the string to evaluate.
  7.  
  8. Output
  9. Print to the standard output
  10.  
  11. Print the character and the amount of times it is encountered in the string in the following format:
  12.  
  13. symbol count
  14. lowercase count
  15. uppercase count
  16. If there is not symbol/lowercase/uppercase character in the string, just output a dash ("-").
  17.  
  18. See sample tests for examples.
  19.  
  20. Sample tests
  21. Input
  22. Telerik Academy rocks!!!
  23. Output
  24. ! 3
  25. e 3
  26. A 1
  27. Input
  28. YES!
  29. Output
  30. ! 1
  31. -
  32. E 1
  33. Input
  34. Yes, English can be weird. It can be understood through tough thorough thought, though.
  35. Output
  36. 13
  37. h 10
  38. E 1
  39.  
  40. import java.util.*;
  41.  
  42. public class Main {
  43. public static void main(String[] args) {
  44. Scanner scanner = new Scanner(System.in);
  45. String input = scanner.nextLine();
  46. HashMap<Character, Integer> upperCaseSymbols = new HashMap<>();
  47. HashMap<Character, Integer> lowerCaseSymbols = new HashMap<>();
  48. HashMap<Character, Integer> symbols = new HashMap<>();
  49. int counter;
  50. for (int i = 0; i < input.length(); i++) {
  51. char symbol = input.charAt(i);
  52. if (Character.isUpperCase(input.charAt(i))) {
  53. if (upperCaseSymbols.containsKey(symbol)) {
  54. counter = upperCaseSymbols.get(symbol);
  55. upperCaseSymbols.put(symbol, counter + 1);
  56. } else {
  57. upperCaseSymbols.putIfAbsent(symbol, 1);
  58. }
  59. } else if (Character.isLowerCase(symbol)) {
  60. if (lowerCaseSymbols.containsKey(symbol)) {
  61. counter = lowerCaseSymbols.get(symbol);
  62. lowerCaseSymbols.put(symbol, counter + 1);
  63. } else {
  64. lowerCaseSymbols.putIfAbsent(symbol, 1);
  65. }
  66. } else {
  67. if (symbols.containsKey(symbol)) {
  68. counter = symbols.get(symbol);
  69. symbols.put(symbol, counter + 1);
  70. } else {
  71. symbols.putIfAbsent(symbol, 1);
  72. }
  73. }
  74. }
  75. int maxSymbolsCount = 0;
  76. int maxUpperCaseCount = 0;
  77. int maxResultForLower = 0;
  78. Character noLetterForSymbols = '-';
  79. Character noLetterForUpper = '-';
  80. Character noLetterForLower = '-';
  81.  
  82. for (Map.Entry<Character, Integer> map : symbols.entrySet()) {
  83. if (map.getValue() > maxSymbolsCount) {
  84. maxSymbolsCount = map.getValue();
  85. noLetterForSymbols = map.getKey();
  86. } else if (map.getValue() == maxSymbolsCount) {
  87. noLetterForSymbols = CheckAscii(map.getKey(), noLetterForSymbols);
  88. }
  89. }
  90. if (maxSymbolsCount == 0) {
  91. System.out.println(noLetterForSymbols);
  92. } else {
  93. System.out.printf("%s %d%n", noLetterForSymbols, maxSymbolsCount);
  94. }
  95. for (Map.Entry<Character, Integer> map : lowerCaseSymbols.entrySet()) {
  96. if (map.getValue() > maxResultForLower) {
  97. maxResultForLower = map.getValue();
  98. noLetterForLower = map.getKey();
  99. } else if (map.getValue() == maxResultForLower){
  100. noLetterForLower = CheckAscii(map.getKey(),noLetterForLower);
  101. }
  102. }
  103. if (maxResultForLower == 0) {
  104. System.out.println(noLetterForLower);
  105. } else {
  106. System.out.printf("%s %d%n", noLetterForLower, maxResultForLower);
  107. }
  108. for (Map.Entry<Character, Integer> map : upperCaseSymbols.entrySet()) {
  109. if (map.getValue() > maxUpperCaseCount) {
  110. maxUpperCaseCount = map.getValue();
  111. noLetterForUpper = map.getKey();
  112. } else if (map.getValue() == maxUpperCaseCount){
  113. noLetterForUpper = CheckAscii(map.getKey(),noLetterForUpper);
  114. }
  115. }
  116. if (maxUpperCaseCount == 0) {
  117. System.out.println(noLetterForUpper);
  118. } else {
  119. System.out.printf("%s %d%n", noLetterForUpper, maxUpperCaseCount);
  120. }
  121. }
  122. public static Character CheckAscii(Character a, Character b){
  123. int numberOfA = a.hashCode();
  124. int numberOfB = b.hashCode();
  125. if (numberOfA < numberOfB){
  126. return a;
  127. } else {
  128. return b;
  129. }
  130. }
  131. }
Advertisement
Add Comment
Please, Sign In to add comment