Advertisement
Ligh7_of_H3av3n

01. Unique Usernames

May 21st, 2024
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.05 KB | None | 0 0
  1. package Uprajnenie;
  2.  
  3. import java.util.Map;
  4. import java.util.Scanner;
  5. import java.util.TreeMap;
  6.  
  7. public class CountSymbols {
  8.     public static void main(String[] args) {
  9.         Scanner scanner = new Scanner(System.in);
  10.  
  11.  
  12.  
  13.         // Read input text
  14.         String text = scanner.nextLine();
  15.  
  16.         // Create a TreeMap to store characters and their counts in alphabetical order
  17.         Map<Character, Integer> charCounts = new TreeMap<>();
  18.  
  19.         // Count occurrences of each character (including spaces and punctuation)
  20.         for (char c : text.toCharArray()) {
  21.             charCounts.put(c, charCounts.getOrDefault(c, 0) + 1);
  22.         }
  23.  
  24.         // Print the results in alphabetical order
  25.         for (Map.Entry<Character, Integer> entry : charCounts.entrySet()) {
  26.             char character = entry.getKey();
  27.             int count = entry.getValue();
  28.             String times = count == 1 ? "time" : "time";
  29.             System.out.println(character + ": " + count + " " + times + "/s");
  30.         }
  31.  
  32.         scanner.close();
  33.     }
  34. }
  35.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement