Thursday, December 26, 2024
Google search engine
HomeLanguagesPython program to find occurrence to each character in given string

Python program to find occurrence to each character in given string

Given a string, the task is to write a program in Python that prints the number of occurrences of each character in a string. There are multiple ways in Python, we can do this task. Let’s discuss a few of them. 

Method #1: Using set() + count() Iterate over the set converted string and get the count of each character in original string. 

Python3




# Python3 code to program to find occurrence
# to each character in given string
 
# initializing string
inp_str = "Lazyroar"
 
# using set() + count() to get count
# of each element in string
out = {x : inp_str.count(x) for x in set(inp_str )}
 
# printing result
print ("Occurrence of all characters in Lazyroar is :\n "+ str(out))


Output

Occurrence of all characters in Lazyroar is :
 {'o': 1, 's': 2, 'e': 4, 'G': 2, 'r': 1, 'f': 1, 'k': 2}

Time Complexity: O(n)
Auxiliary Space: O(n)

  Method #2: Using dictionary 

Python3




# Python3 code to program to find occurrence
# to each character in given string
 
# initializing string
inp_str = "Lazyroar"
 
# frequency dictionary
freq = {}
   
for ele in inp_str:
    if ele in freq:
        freq[ele] += 1
    else:
        freq[ele] = 1
   
# printing result 
print ("Occurrence of all characters in Lazyroar is :\n "+ str(freq))


Output

Occurrence of all characters in Lazyroar is :
 {'G': 2, 'e': 4, 'k': 2, 's': 2, 'f': 1, 'o': 1, 'r': 1}

Time Complexity: O(n)
Auxiliary Space: O(n)

  Method #3: Using collections 

Python3




# Python3 code to program to find occurrence
# to each character in given string
from collections import Counter
   
# initializing string 
in_str = "Lazyroar"
   
# using collections.Counter() to get 
# count of each element in string 
oup = Counter(in_str)
   
# printing result 
print ("Occurrence of all characters in Lazyroar is :\n "+ str(oup))


Output

Occurrence of all characters in Lazyroar is :
 Counter({'e': 4, 'G': 2, 'k': 2, 's': 2, 'f': 1, 'o': 1, 'r': 1})

Time Complexity: O(n)
Auxiliary Space: O(n)

 Method #4: Using Iteration and get method

This code is finding the number of occurrences of each character in the string . It does this by iterating through the characters in the string, and keeping a count of each character in a dictionary called “freq”. For each character, it increments the count of that character in the dictionary by 1. At the end, it prints the dictionary containing the counts of each character.

Python3




#Python3 code to program to find occurrence
#to each character in given string
#initializing string
inp_str = "Lazyroar"
 
#frequency dictionary
freq = {}
 
for ele in inp_str:
  freq[ele] = freq.get(ele, 0) + 1
 
#printing result
print ("Occurrence of all characters in Lazyroar is :\n "+ str(freq))
#This code is contributed by Edula Vinay Kumar Reddy


Output

Occurrence of all characters in Lazyroar is :
 {'G': 2, 'e': 4, 'k': 2, 's': 2, 'f': 1, 'o': 1, 'r': 1}

Time Complexity: The time complexity of this code is O(n), where n is the length of the input string. This is because the for loop iterates through all elements of the string and the get() method has a time complexity of O(1).
Auxiliary Space: The space complexity of this code is O(k), where k is the number of unique characters in the input string. This is because the frequency dictionary stores the count for each unique character in the string.

Method: Using the setdefault() method

  • A dictionary freq is defined.
  • for loop is used to iterate over each character in the string “GeeksForGeeks”.
  • the setdefault() method is used to set the value of character in the dictionary to 0 if it does not exist already.
  • Then, the value of the character in the dictionary is incremented by 1.
  • After the loop completes, the freq dictionary contains the occurrence of each character in a string.

Python3




string = "GeeksForGeeks"
freq = {}
 
for char in string:
    freq.setdefault(char, 0)
    freq[char] += 1
 
print ("Occurrence of all characters in Lazyroar is :\n "+ str(freq))


Output

Occurrence of all characters in Lazyroar is :
 {'G': 2, 'e': 4, 'k': 2, 's': 2, 'F': 1, 'o': 1, 'r': 1}

Time Complexity: O(n) as we are the traversing the completes string where n is the length of the string  
Auxiliary Space: O(K) The space complexity of this code is O(k), where k is the number of unique characters in the input string. This is because the frequency dictionary stores the count for each unique character in the string.

Method #6 : Using operator.countOf() method

Approach

  1. Get unique characters from string by using list(),set() methods
  2. Create a new dictionary with key as unique value and count of unique value in original string as value using operator.countOf() method
  3. Display the dictionary

Python3




# Python3 code to program to find occurrence
 
# to each character in given string
 
 
 
# initializing string
 
inp_str = "Lazyroar"
 
x=list(set(inp_str))
 
out = dict()
 
import operator
 
for i in x:
 
    out[i]=operator.countOf(inp_str,i)
 
# printing result
 
print ("Occurrence of all characters in Lazyroar is :\n "+ str(out))


Output

Occurrence of all characters in Lazyroar is :
 {'G': 2, 'k': 2, 's': 2, 'e': 4, 'r': 1, 'o': 1, 'f': 1}

Time Complexity: O(n) as we are the traversing the completes string where n is the length of the string  
Auxiliary Space: O(K) The space complexity of this code is O(k), where k is the number of unique characters in the input string. This is because the frequency dictionary stores the count for each unique character in the string.

RELATED ARTICLES

Most Popular

Recent Comments