Tutorialspoint
Problem
Solution
Submissions

Frequency of Characters in a String

Certification: Basic Level Accuracy: 40% Submissions: 5 Points: 5

Write a C# program to implement the CharacterFrequency(string s) function, which counts the frequency of each character in a given string. The function should return a dictionary where the keys are characters and the values are their frequencies.

Example 1
  • Input: s = "hello"
  • Output: {'h': 1, 'e': 1, 'l': 2, 'o': 1}
  • Explanation:
    • 'h' appears 1 time
    • 'e' appears 1 time
    • 'l' appears 2 times
    • 'o' appears 1 time
Example 2
  • Input: s = "programming"
  • Output: {'p': 1, 'r': 2, 'o': 1, 'g': 2, 'a': 1, 'm': 2, 'i': 1, 'n': 1}
  • Explanation:
    • 'p' appears 1 time
    • 'r' appears 2 times
    • 'o' appears 1 time
    • 'g' appears 2 times
    • 'a' appears 1 time
    • 'm' appears 2 times
    • 'i' appears 1 time
    • 'n' appears 1 time
Constraints
  • 0 ≤ s.length ≤ 10^5
  • s consists of English letters (lower-case and upper-case), digits (0-9), and spaces
  • Time Complexity: O(n)
  • Space Complexity: O(k), where k is the number of unique characters in the string
StringsDictionaries CapgeminiApple
Editorial

Login to view the detailed solution and explanation for this problem.

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.

Please Login to continue
Solve Problems

 
 
 
Output Window

Don't have an account? Register

Solution Hints

  • Use a dictionary/hashtable to store character frequencies
  • Iterate through each character in the string and update its count
  • Consider handling case sensitivity based on requirements
  • Think about how to handle spaces and special characters


Submitted Code :