Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- 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.
- Input
- Read from the standard input
- There is a single line of input - the string to evaluate.
- Output
- Print to the standard output
- Print the character and the amount of times it is encountered in the string in the following format:
- symbol count
- lowercase count
- uppercase count
- If there is not symbol/lowercase/uppercase character in the string, just output a dash ("-").
- See sample tests for examples.
- Sample tests
- Input
- Telerik Academy rocks!!!
- Output
- ! 3
- e 3
- A 1
- Input
- YES!
- Output
- ! 1
- -
- E 1
- Input
- Yes, English can be weird. It can be understood through tough thorough thought, though.
- Output
- 13
- h 10
- E 1
- import java.util.*;
- public class Main {
- public static void main(String[] args) {
- Scanner scanner = new Scanner(System.in);
- String input = scanner.nextLine();
- HashMap<Character, Integer> upperCaseSymbols = new HashMap<>();
- HashMap<Character, Integer> lowerCaseSymbols = new HashMap<>();
- HashMap<Character, Integer> symbols = new HashMap<>();
- int counter;
- for (int i = 0; i < input.length(); i++) {
- char symbol = input.charAt(i);
- if (Character.isUpperCase(input.charAt(i))) {
- if (upperCaseSymbols.containsKey(symbol)) {
- counter = upperCaseSymbols.get(symbol);
- upperCaseSymbols.put(symbol, counter + 1);
- } else {
- upperCaseSymbols.putIfAbsent(symbol, 1);
- }
- } else if (Character.isLowerCase(symbol)) {
- if (lowerCaseSymbols.containsKey(symbol)) {
- counter = lowerCaseSymbols.get(symbol);
- lowerCaseSymbols.put(symbol, counter + 1);
- } else {
- lowerCaseSymbols.putIfAbsent(symbol, 1);
- }
- } else {
- if (symbols.containsKey(symbol)) {
- counter = symbols.get(symbol);
- symbols.put(symbol, counter + 1);
- } else {
- symbols.putIfAbsent(symbol, 1);
- }
- }
- }
- int maxSymbolsCount = 0;
- int maxUpperCaseCount = 0;
- int maxResultForLower = 0;
- Character noLetterForSymbols = '-';
- Character noLetterForUpper = '-';
- Character noLetterForLower = '-';
- for (Map.Entry<Character, Integer> map : symbols.entrySet()) {
- if (map.getValue() > maxSymbolsCount) {
- maxSymbolsCount = map.getValue();
- noLetterForSymbols = map.getKey();
- } else if (map.getValue() == maxSymbolsCount) {
- noLetterForSymbols = CheckAscii(map.getKey(), noLetterForSymbols);
- }
- }
- if (maxSymbolsCount == 0) {
- System.out.println(noLetterForSymbols);
- } else {
- System.out.printf("%s %d%n", noLetterForSymbols, maxSymbolsCount);
- }
- for (Map.Entry<Character, Integer> map : lowerCaseSymbols.entrySet()) {
- if (map.getValue() > maxResultForLower) {
- maxResultForLower = map.getValue();
- noLetterForLower = map.getKey();
- } else if (map.getValue() == maxResultForLower){
- noLetterForLower = CheckAscii(map.getKey(),noLetterForLower);
- }
- }
- if (maxResultForLower == 0) {
- System.out.println(noLetterForLower);
- } else {
- System.out.printf("%s %d%n", noLetterForLower, maxResultForLower);
- }
- for (Map.Entry<Character, Integer> map : upperCaseSymbols.entrySet()) {
- if (map.getValue() > maxUpperCaseCount) {
- maxUpperCaseCount = map.getValue();
- noLetterForUpper = map.getKey();
- } else if (map.getValue() == maxUpperCaseCount){
- noLetterForUpper = CheckAscii(map.getKey(),noLetterForUpper);
- }
- }
- if (maxUpperCaseCount == 0) {
- System.out.println(noLetterForUpper);
- } else {
- System.out.printf("%s %d%n", noLetterForUpper, maxUpperCaseCount);
- }
- }
- public static Character CheckAscii(Character a, Character b){
- int numberOfA = a.hashCode();
- int numberOfB = b.hashCode();
- if (numberOfA < numberOfB){
- return a;
- } else {
- return b;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment