Open In App

Python – Extract indices of Present, Non Index matching Strings

Improve
Improve
Like Article
Like
Save
Share
Report

Given two strings, extract indices of all characters from string 1 which are present in the other string, but not in the same index.

Input : test_str1 = ‘pplg’, test_str2 = ‘pineapple’ 
Output : [0, 1, 2] 
Explanation : ppl is found in 2nd string, also not on same index as 1st.

Input : test_str1 = ‘pine’, test_str2 = ‘pineapple’ 
Output : [] 
Explanation : Found in other string on same index.

Method #1 : Using enumerate() + loop

In this, we employ a nested loop to check for each character its occurrence in 2nd string, and then if it’s in other position, if found the index is appended.

Python3




# Python3 code to demonstrate working of
# Extract indices of Present, Non Index matching Strings
# using loop + enumerate()
 
# initializing strings
test_str1 = 'apple'
test_str2 = 'pineapple'
 
# printing original Strings
print("The original string 1 is : " + str(test_str1))
print("The original string 2 is : " + str(test_str2))
 
# the replaced result
res = []
for idx, val in enumerate(test_str1):
     
    # if present in string 2
    if val in test_str2:
         
        # if not present at same index
        if test_str2[idx] != val:     
            res.append(idx)
 
# printing result
print("The extracted indices : " + str(res))


Output

The original string 1 is : apple
The original string 2 is : pineapple
The extracted indices : [0, 1, 2, 3, 4]

Method #2 : Using enumerate() + zip() + list comprehension

In this, we perform the task of getting indices using enumerate() and pairing of both strings is done using zip(), the conditional checks occur using list comprehension.

Python3




# Python3 code to demonstrate working of
# Extract indices of Present, Non Index matching Strings
# using enumerate() + zip() + list comprehension
 
# initializing strings
test_str1 = 'apple'
test_str2 = 'pineapple'
 
# printing original Strings
print("The original string 1 is : " + str(test_str1))
print("The original string 2 is : " + str(test_str2))
 
# one-liner to solve this problem.
res = [idx for idx, (x, y) in enumerate(
    zip(test_str1, test_str2)) if x != y and x in test_str2]
 
# printing result
print("The extracted indices : " + str(res))


Output

The original string 1 is : apple
The original string 2 is : pineapple
The extracted indices : [0, 1, 2, 3, 4]

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

Method #3: Use a for loop

  • Initialize two empty lists, one for storing indices of present strings and another for storing indices of non-index matching strings.
  • Iterate over each character and its index simultaneously using the built-in enumerate function.
  • If the character from the first string is in the second string, append its index to the present strings list.
  • If the characters at the same index from both strings are different, append the index to the non-index matching strings list.
  • Print the final results.

Python3




# Python3 code to demonstrate an alternative approach for
# Extracting indices of Present, Non Index matching Strings
 
# initializing strings
test_str1 = 'apple'
test_str2 = 'pineapple'
 
# printing original Strings
print("The original string 1 is : " + str(test_str1))
print("The original string 2 is : " + str(test_str2))
 
# initialize two empty lists
present_strs = []
non_index_matching_strs = []
 
# iterate over each character and its index using enumerate
for idx, char in enumerate(test_str1):
     
    # check if character is present in the second string
    if char in test_str2:
        present_strs.append(idx)
     
    # check if characters at the same index are different
    if char != test_str2[idx]:
        non_index_matching_strs.append(idx)
 
# printing results
print("Indices of Present strings : ", present_strs)
print("Indices of Non Index matching strings : ", non_index_matching_strs)


Output

The original string 1 is : apple
The original string 2 is : pineapple
Indices of Present strings :  [0, 1, 2, 3, 4]
Indices of Non Index matching strings :  [0, 1, 2, 3, 4]

Time complexity: O(n), where n is the length of the first string.
Auxiliary space: O(k), where k is the number of indices that satisfy the conditions.

Method #4: Using a set intersection 

  • Create two strings test_str1 and test_str2.
  • Print the original strings test_str1 and test_str2.
  • Find the common characters between the two strings using set intersection and store them in the common_chars set.
  • Create a list present_strs which stores the indices of characters in test_str1 that are present in common_chars.
  • Create a list of non_index_matching_strs which stores the indices of characters in test_str1 that are not present in test_str2 or are not in the same position in both strings.
  • Print the result by displaying the present_strs and non_index_matching_strs

Python3




test_str1 = 'apple'
test_str2 = 'pineapple'
 
# printing original Strings
print("The original string 1 is : " + str(test_str1))
print("The original string 2 is : " + str(test_str2))
 
# using set intersection and list comprehension
common_chars = set(test_str1) & set(test_str2)
present_strs = [idx for idx, char in enumerate(test_str1) if char in common_chars]
non_index_matching_strs = [idx for idx, char in enumerate(test_str1) if char not in test_str2 or char != test_str2[idx]]
 
# printing results
print("Indices of Present strings : ", present_strs)
print("Indices of Non Index matching strings : ", non_index_matching_strs)


Output

The original string 1 is : apple
The original string 2 is : pineapple
Indices of Present strings :  [0, 1, 2, 3, 4]
Indices of Non Index matching strings :  [0, 1, 2, 3, 4]

Time complexity: O(n), where n is the length of the first string test_str1. 
Auxiliary space: O(n), as the present_strs and non_index_matching_strs lists can potentially store up to n elements each.

Method #5: set comprehension and zip() functions

Python3




# Python3 code to demonstrate another alternative approach for
# Extracting indices of Present, Non Index matching Strings
 
# initializing strings
test_str1 = 'apple'
test_str2 = 'pineapple'
 
# printing original Strings
print("The original string 1 is : " + str(test_str1))
print("The original string 2 is : " + str(test_str2))
 
# using set comprehension to get the indices of present characters
present_strs = {idx for idx, char in enumerate(test_str1) if char in test_str2}
 
# using set comprehension to get the indices of non-index matching characters
non_index_matching_strs = {idx for idx, (char1, char2) in enumerate(zip(test_str1, test_str2)) if char1 != char2}
 
# printing results
print("Indices of Present strings : ", present_strs)
print("Indices of Non Index matching strings : ", non_index_matching_strs)


Output

The original string 1 is : apple
The original string 2 is : pineapple
Indices of Present strings :  {0, 1, 2, 3, 4}
Indices of Non Index matching strings :  {0, 1, 2, 3, 4}

Time complexity: O(n), where n is the length of the input strings.
Auxiliary space: O(n)



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