
Problem
Solution
Submissions
Characters in a String
Certification: Basic Level
Accuracy: 85.71%
Submissions: 7
Points: 5
Write a Java program that counts the frequency of each character in a given string. Return a HashMap where keys are characters and values are their frequencies. Ignore spaces, and case is sensitive.
Example 1
- Input: s = "Hello World"
- Output: {H=1, e=1, l=3, o=2, W=1, r=1, d=1}
- Explanation:
- Spaces ignored, counts reflect correct frequencies
Example 2
- Input: s = "programming"
- Output: {p=1, r=1, o=1, g=2, a=1, m=2, i=1, n=1}
Constraints
- 1 ≤ s.length ≤ 10^5
- Ignore space character, preserve case
- Time Complexity: O(n)
- Space Complexity: O(k), k = unique characters
Editorial
My Submissions
All Solutions
Lang | Status | Date | Code |
---|---|---|---|
You do not have any submissions for this problem. |
User | Lang | Status | Date | Code |
---|---|---|---|---|
No submissions found. |
Solution Hints
- Use a HashMap to store characters as keys and their frequencies as values.
- Iterate through each character in the string.
- Skip space characters.
- For each non-space character, update its frequency in the HashMap.
- Return the resulting HashMap.