Python - Characters Index occurrences in String
Last Updated :
23 Apr, 2023
Sometimes, while working with Python Strings, we can have a problem in which we need to check for all the characters indices. The position where they occur. This kind of application can come in many domains. Lets discuss certain ways in which this task can be performed.
Method #1 : Using set() + regex + list comprehension + replace() The combination of above functions can be used to perform this task. In this, set() is used to get elements whose frequency has to be computed. The task of assembling to dictionary accordingly is performed using regex function and list comprehension.
Python3
# Python3 code to demonstrate working of
# Characters Index occurrences in String
# Using regex + set() + list comprehension + replace()
import re
# initializing string
test_str = "Gfg is best for geeks"
# printing original string
print("The original string is : " + test_str)
# Characters Index occurrences in String
# Using regex + set() + list comprehension + replace()
temp = set(test_str.replace(' ', ''))
res = {ele: [sub.start() for sub in re.finditer(ele, test_str)] for ele in temp}
# printing result
print("Characters frequency index dictionary : " + str(res))
OutputThe original string is : Gfg is best for geeks
Characters frequency index dictionary : {'G': [0], 'e': [8, 17, 18], 'g': [2, 16], 't': [10], 's': [5, 9, 20], 'i': [4], 'k': [19], 'b': [7], 'r': [14], 'o': [13], 'f': [1, 12]}
Method #2 : Using loop + enumerate() This is yet another way in which this task can be performed. In this we create a dictionary and then iterate the string to map the characters with their respective characters.
Python3
# Python3 code to demonstrate working of
# Characters Index occurrences in String
# Using loop + enumerate()
import re
# initializing string
test_str = "Gfg is best for geeks"
# printing original string
print("The original string is : " + test_str)
# Characters Index occurrences in String
# Using loop + enumerate()
res = {ele : [] for ele in test_str}
for idx, ele in enumerate(test_str):
res[ele].append(idx)
# printing result
print("Characters frequency index dictionary : " + str(res))
OutputThe original string is : Gfg is best for geeks
Characters frequency index dictionary : {'G': [0], 'f': [1, 12], 'g': [2, 16], ' ': [3, 6, 11, 15], 'i': [4], 's': [5, 9, 20], 'b': [7], 'e': [8, 17, 18], 't': [10], 'o': [13], 'r': [14], 'k': [19]}
The Time and Space Complexity for all the methods are the same:
Time Complexity: O(n)
Auxiliary Space: O(n)
Method #3 : Using split(),join(),list() and set() methods
Python3
# Python3 code to demonstrate working of
# Characters Index occurrences in String
# initializing string
test_str = "Gfg is best for geeks"
# printing original string
print("The original string is : " + test_str)
# Characters Index occurrences in String
x=test_str.split()
x="".join(x)
x=list(set(x))
res=dict()
for i in x:
a=[]
for j in range(0,len(test_str)):
if(i==test_str[j]):
a.append(j)
res[i]=a
# printing result
print("Characters frequency index dictionary : " + str(res))
OutputThe original string is : Gfg is best for geeks
Characters frequency index dictionary : {'i': [4], 'f': [1, 12], 's': [5, 9, 20], 'o': [13], 'g': [2, 16], 'e': [8, 17, 18], 'G': [0], 'r': [14], 't': [10], 'k': [19], 'b': [7]}
Time complexity : O(n*m), where n is length of list x and m is length of test_str.
Auxiliary space : O(n), where n is length of res dictionary.
Method #4:Using map() and filter()
Here's the step by step algorithm
- Initialize the string test_str with the value "Gfg is best for geeks".
- Create a set of all unique characters in test_str using set(test_str). This will return a set of unique characters in the string, i.e., {'G', 'f', 'g', 'i', 's', 'b', 'e', 't', 'o', 'r', 'k'}.
- For each unique character in the set, create a new list of indices where that character appears in the string.
- To create this list, use the map() function to apply a lambda function to each tuple returned by enumerate(test_str). The lambda function takes a tuple (i, char) and returns i if the character matches the current unique character being processed.
- Use the filter() function to remove all tuples where the character does not match the current unique character being processed.
- Use the list() function to convert the map() and filter() objects into lists of indices.
- Create a key-value pair in the dictionary res where the key is the unique character being processed and the value is the list of indices where the character appears in the string.
- Print the resulting dictionary res which will contain a mapping of each unique character in test_str to a list of indices where it appears in the string.
Python3
test_str = "Gfg is best for geeks"
# printing original string
print("The original string is : " + test_str)
res = {char: list(map(lambda x: x[0], filter(lambda x: x[1] == char, enumerate(test_str)))) for char in set(test_str)}
# printing result
print("Characters frequency index dictionary : " + str(res))
#This code is countributed by Vinay Pinjala.
OutputThe original string is : Gfg is best for geeks
Characters frequency index dictionary : {'s': [5, 9, 20], 'k': [19], 'b': [7], 'i': [4], 'o': [13], 'G': [0], ' ': [3, 6, 11, 15], 't': [10], 'r': [14], 'e': [8, 17, 18], 'f': [1, 12], 'g': [2, 16]}
Time complexity:
The time complexity of the code is O(nm), where n is the length of the string test_str and m is the number of unique characters in test_str. This is because we need to iterate through each character in test_str at least once, and for each character, we are performing a filter operation over the enumerated string which takes O(n) time, as well as a map operation over the filtered list which also takes O(n) time. Since we are doing this for each unique character in the string, the time complexity becomes O(nm).
Space complexity:
The space complexity of the code is O(nm), where n is the length of the string test_str and m is the number of unique characters in test_str. This is because we are creating a dictionary res where the keys are the unique characters in the string, and the values are lists of indices where each character appears. The size of this dictionary will be proportional to the number of unique characters in the string, which is at most n. For each unique character, we are storing a list of indices where that character appears, which can be at most n as well. Therefore, the space complexity becomes O(nm).
Similar Reads
Python | First character occurrence from rear String
There are many ways to find out the first index of element in String as python in its language provides index() function that returns the index of first occurrence of element in String. But if one desires to get the last occurrence of element in string, usually a longer method has to be applied. Let
4 min read
Python - Lowercase Kth Character in string
The problem of lowercasing a string is quite common and has been discussed many times. But sometimes, we might have a problem like this in which we need to convert the Nth character of string to lowercase. Letâs discuss certain ways in which this can be performed. Method #1 : Using string slicing +
4 min read
Python - Mid occurrence of K in string
Given a String, the task is to write a Python program to extract the mid occurrence of a character. Input : test_str = "geeksforgeeks is best for all geeks", K = 'e' Output : 10 Explanation : 7 occurrences of e. The 4th occurrence [mid] is at 10th index. Input : test_str = "geeksforgeeks is best for
6 min read
Iterate over characters of a string in Python
In this article, we will learn how to iterate over the characters of a string in Python. There are several methods to do this, but we will focus on the most efficient one. The simplest way is to use a loop. Letâs explore this approach.Using for loopThe simplest way to iterate over the characters in
2 min read
Python - Expand Character Frequency String
Given a string, which characters followed by its frequency, create the appropriate string. Examples: Input : test_str = 'g7f2g3i2s2b3e4' Output : gggggggffgggiissbbbeeee Explanation : g is succeeded by 7 and repeated 7 times. Input : test_str = 'g1f1g1' Output : gfg Explanation : f is succeeded by 1
4 min read
Character Indices Mapping in String List - Python
We are given a string list we need to map characters to their indices. For example, a = ["hello", "world"] so that output should be [[('h', 0), ('e', 1), ('l', 2), ('l', 3), ('o', 4)], [('w', 0), ('o', 1), ('r', 2), ('l', 3), ('d', 4)]].Using a nested for loopA nested for loop iterates through each
3 min read
Python - Least Frequent Character in String
The task is to find the least frequent character in a string, we count how many times each character appears and pick the one with the lowest count.Using collections.CounterThe most efficient way to do this is by using collections.Counter which counts character frequencies in one go and makes it eas
3 min read
Python | Substitute character with its occurrence
Sometimes, while working with Python, we can have a problem in which we need to substitute a character with its occurrence in a string. This a peculiar problem but can have application in many domains. Lets discuss certain ways in which this task can be performed. Method #1 : Using loop This is brut
6 min read
Python - Characters which Occur in More than K Strings
Sometimes, while working with Python, we have a problem in which we compute how many characters are present in string. But sometimes, we can have a problem in which we need to get all characters that occur in atleast K Strings. Let's discuss certain ways in which this task can be performed. Method #
4 min read
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
5 min read