Python – Extract indices of Present, Non Index matching Strings
Last Updated :
10 May, 2023
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
test_str1 = 'apple'
test_str2 = 'pineapple'
print ( "The original string 1 is : " + str (test_str1))
print ( "The original string 2 is : " + str (test_str2))
res = []
for idx, val in enumerate (test_str1):
if val in test_str2:
if test_str2[idx] ! = val:
res.append(idx)
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
test_str1 = 'apple'
test_str2 = 'pineapple'
print ( "The original string 1 is : " + str (test_str1))
print ( "The original string 2 is : " + str (test_str2))
res = [idx for idx, (x, y) in enumerate (
zip (test_str1, test_str2)) if x ! = y and x in test_str2]
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
test_str1 = 'apple'
test_str2 = 'pineapple'
print ( "The original string 1 is : " + str (test_str1))
print ( "The original string 2 is : " + str (test_str2))
present_strs = []
non_index_matching_strs = []
for idx, char in enumerate (test_str1):
if char in test_str2:
present_strs.append(idx)
if char ! = test_str2[idx]:
non_index_matching_strs.append(idx)
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'
print ( "The original string 1 is : " + str (test_str1))
print ( "The original string 2 is : " + str (test_str2))
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]]
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
test_str1 = 'apple'
test_str2 = 'pineapple'
print ( "The original string 1 is : " + str (test_str1))
print ( "The original string 2 is : " + str (test_str2))
present_strs = {idx for idx, char in enumerate (test_str1) if char in test_str2}
non_index_matching_strs = {idx for idx, (char1, char2) in enumerate ( zip (test_str1, test_str2)) if char1 ! = char2}
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)
Share your thoughts in the comments
Please Login to comment...