Open In App

Python | Count of Matching i, j index elements

Last Updated : 09 Apr, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

Sometimes, while programming, we can have a problem in which we need to check for ith and jth character of each string. We may require to extract count of all strings with similar ith and jth characters. Let’s discuss certain ways in which this task can be performed. 

Method #1 : Using loop This is brute force method by which this task can be performed. In this, iterate each element of list and check for each string’s ith and jth character and increase the counter in case we find a match. 

Python3




# Python3 code to demonstrate working of
# Count of Matching i, j index elements
# Using loop
 
# initialize list
test_list = ['geeks', 'beke', 'treat', 'neke']
 
# printing original list
print("The original list : " + str(test_list))
 
# initialize i
i = 1
 
# initialize j
j = 3
 
# Count of Matching i, j index elements
# Using loop
count = 0
for ele in test_list:
    if ele[i] == ele[j]:
        count = count + 1
 
# printing result
print("Total Strings with similar ith and jth elements : " + str(count))


Output : 

The original list : ['geeks', 'beke', 'treat', 'neke']
Total Strings with similar ith and jth elements : 2

Time Complexity: O(n), where n is the length of the input list. This is because we’re using the loop which has a time complexity of O(n) in the worst case.
Auxiliary Space: O(n), as we’re using additional space res other than the input list itself with the same size of input list.

  Method #2 : Using sum() + generator expression This is one liner alternative to perform this task. In this, we perform the task of iteration using generator expression and summation using sum(). 

Python3




# Python3 code to demonstrate working of
# Count of Matching i, j index elements
# Using sum() + generator expression
 
# initialize list
test_list = ['geeks', 'beke', 'treat', 'neke']
 
# printing original list
print("The original list : " + str(test_list))
 
# initialize i
i = 1
 
# initialize j
j = 3
 
# Count of Matching i, j index elements
# Using sum() + generator expression
res = sum(1 for ele in test_list if ele[i] == ele[j])
 
# printing result
print("Total Strings with similar ith and jth elements : " + str(res))


Output : 

The original list : ['geeks', 'beke', 'treat', 'neke']
Total Strings with similar ith and jth elements : 2

Time Complexity: O(n) where n is the number of elements in the string list. The sum() + generator expression is used to perform the task and it takes O(n) time.
Auxiliary Space: O(1) additional constant space is required

Method #3 :  Another approach can be using filter() function and lambda function.

Python3




# Python3 code to demonstrate working of
# Count of Matching i, j index elements
# Using filter() + lambda
   
# initialize list
test_list = ['geeks', 'beke', 'treat', 'neke']
   
# printing original list
print("The original list : " + str(test_list))
   
# initialize i
i = 1
   
# initialize j
j = 3
   
# Count of Matching i, j index elements
# Using filter() + lambda
result = len(list(filter(lambda x: x[i] == x[j], test_list)))
   
# printing result
print("Total Strings with similar ith and jth elements : " + str(result))
#This code is contributed by Edula Vinay Kumar Reddy


Output

The original list : ['geeks', 'beke', 'treat', 'neke']
Total Strings with similar ith and jth elements : 2

In this approach, we are using the filter() function along with a lambda function to filter out elements from the list test_list that match the condition of having the same value at index i and index j. We then use the len() function to count the number of elements in the filtered list, which gives us the total count of strings with similar ith and jth elements. The time complexity of this approach is O(n) as we are iterating through the list once and the space complexity is O(n) as we are creating a new filtered list.



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads