Open In App

Python – Characters Index occurrences in String

Improve
Improve
Like Article
Like
Save
Share
Report

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))


Output

The 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))


Output

The 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))


Output

The 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

  1. Initialize the string test_str with the value “Gfg is best for geeks”.
  2. 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’}.
  3. For each unique character in the set, create a new list of indices where that character appears in the string.
  4. 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.
  5. Use the filter() function to remove all tuples where the character does not match the current unique character being processed.
  6. Use the list() function to convert the map() and filter() objects into lists of indices.
  7. 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.
  8. 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.


Output

The 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).



Last Updated : 23 Apr, 2023
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads