Skip to content

Latest commit

 

History

History
29 lines (23 loc) · 760 Bytes

1.Sorted_Frequencies.md

File metadata and controls

29 lines (23 loc) · 760 Bytes

Sorted Frequencies

Problem Statement

Print each character and its frequency in sorted order

Algorithm

  • We use the ASCII value of the character and build hashtable based on array index and store the counts there

Code

 # Define the string
str = "geeksforgeeks"

# Initialize the count array
count = [0]*26

# Loop through each character in the string and update the count array
for i in range(len(str)):
    count[ord(str[i]) - ord('a')] += 1

# Loop through the count array and print the character and its count
for i in range(26):
    if count[i] > 0:
        print(chr(i + ord('a')), count[i])