Friday, December 27, 2024
Google search engine
HomeLanguagesPython – Case Insensitive string counter

Python – Case Insensitive string counter

Given a list of strings, find the frequency of strings case insensitive.

Input : test_list = ["Gfg", "Best", "GFG", "is", "IS", "BEST"] 
Output : {'gfg': 2, 'best': 2, 'is': 2} 
Explanation : All occur twice. 
Input : test_list = ["Gfg", "gfg", "GFG"] 
Output : {'gfg': 3} 
Explanation : Only "gfg" 3 occurrences.

Method 1: Using defaultdict() + lower()

In this, we perform lower() to all the strings, before mapping in defaultdict. This ensures case insensitivity while mapping and cumulating frequency.

Python3




# Python3 code to demonstrate working of
# Strings Frequency (Case Insensitive)
# Using defaultdict() + lower()
 
from collections import defaultdict
 
# initializing list
test_list = ["Gfg", "Best", "best", "gfg", "GFG", "is", "IS", "BEST"]
 
# printing original list
print("The original list is : " + str(test_list))
 
res = defaultdict(int)
 
for ele in test_list:
 
    # lowercasing to cater for Case Insensitivity
    res[ele.lower()] += 1
 
# printing result
print("Strings Frequency : " + str(dict(res)))


Output

The original list is : ['Gfg', 'Best', 'best', 'gfg', 'GFG', 'is', 'IS', 'BEST']
Strings Frequency : {'gfg': 3, 'best': 3, 'is': 2}

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

Method 2: Using count() method

Python3




# Python3 code to demonstrate working of
# Strings Frequency (Case Insensitive)
 
# initializing list
test_list = ["Gfg", "Best", "best", "gfg", "GFG", "is", "IS", "BEST"]
 
# printing original list
print("The original list is : " + str(test_list))
x = []
res = dict()
for i in test_list:
    x.append(i.lower())
a = list(set(x))
for i in a:
    res[i] = x.count(i)
 
# printing result
print("Strings Frequency : " + str(dict(res)))


Output

The original list is : ['Gfg', 'Best', 'best', 'gfg', 'GFG', 'is', 'IS', 'BEST']
Strings Frequency : {'is': 2, 'best': 3, 'gfg': 3}

Time Complexity: O(n)

Auxiliary Space: O(n)

Method 3: Using Counter() function

Python3




# Python3 code to demonstrate working of
# Strings Frequency (Case Insensitive)
from collections import Counter
# initializing list
test_list = ["Gfg", "Best", "best", "gfg", "GFG", "is", "IS", "BEST"]
 
# printing original list
print("The original list is : " + str(test_list))
for i in range(len(test_list)):
    test_list[i] = test_list[i].lower()
freq = Counter(test_list)
 
# printing result
print("Strings Frequency : " + str(dict(freq)))


Output

The original list is : ['Gfg', 'Best', 'best', 'gfg', 'GFG', 'is', 'IS', 'BEST']
Strings Frequency : {'gfg': 3, 'best': 3, 'is': 2}

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

Method 4:  using operator.countOf() method

Python3




# Python3 code to demonstrate working of
# Strings Frequency (Case Insensitive)
import operator as op
# initializing list
test_list = ["Gfg", "Best", "best", "gfg", "GFG", "is", "IS", "BEST"]
 
# printing original list
print("The original list is : " + str(test_list))
x = []
res = dict()
for i in test_list:
    x.append(i.lower())
a = list(set(x))
for i in a:
    res[i] = op.countOf(x,i)
 
# printing result
print("Strings Frequency : " + str(dict(res)))


Output

The original list is : ['Gfg', 'Best', 'best', 'gfg', 'GFG', 'is', 'IS', 'BEST']
Strings Frequency : {'is': 2, 'gfg': 3, 'best': 3}

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

Method 5: Using a dictionary and for loop

Step-by-Step approach:

  1. Create an empty dictionary to store the frequency of strings.
  2. Loop through the strings in the list.
  3. Convert each string to lowercase using the str.lower() method.
  4. If the lowercase string is already in the dictionary, increase its value by 1.
  5. If the lowercase string is not in the dictionary, add it as a key with a value of 1.
  6. Print the dictionary.

Python3




# Strings Frequency (Case Insensitive)
# initializing list
test_list = ["Gfg", "Best", "best", "gfg", "GFG", "is", "IS", "BEST"]
 
# printing original list
print("The original list is : " + str(test_list))
 
# create an empty dictionary to store the frequency of strings
freq_dict = {}
 
# loop through the strings in the list
for string in test_list:
    # convert each string to lowercase
    lowercase_string = string.lower()
     
    # if the lowercase string is already in the dictionary, increase its value by 1
    if lowercase_string in freq_dict:
        freq_dict[lowercase_string] += 1
    # if the lowercase string is not in the dictionary, add it as a key with a value of 1
    else:
        freq_dict[lowercase_string] = 1
 
# printing result
print("Strings Frequency : " + str(freq_dict))


Output

The original list is : ['Gfg', 'Best', 'best', 'gfg', 'GFG', 'is', 'IS', 'BEST']
Strings Frequency : {'gfg': 3, 'best': 3, 'is': 2}

Time Complexity: O(n), where n is the number of strings in the list.

Auxiliary Space: O(n), where n is the number of strings in the list, to store the dictionary.

Dominic Rubhabha-Wardslaus
Dominic Rubhabha-Wardslaushttp://wardslaus.com
infosec,malicious & dos attacks generator, boot rom exploit philanthropist , wild hacker , game developer,
RELATED ARTICLES

Most Popular

Recent Comments