Menu Close

Return all strings in words which is sub-string of another word in any order.

Given an array of string words. Return all strings in words which is sub-string of another word in any order. String words[i] is sub-string of words[j], if can be obtained removing some characters to left and/or right side of words[j].
Input: words = [‘mass’,’as’,’hero’,’superhero’]
Output: [‘as’,’hero’]

words=list(map(str,input('Enter the array of strings : ').split(' ')))
i,L=0,[]
while i<len(words):
    j=0
    while j<len(words):
        if words[i]!=words[j]:
            if words[i] in words[j]:
                L.append(words[i])
        j+=1
    i+=1
print(L)

Input_1:
Enter the array of strings: mass as hero superhero
Output:
[‘as’, ‘hero’]


Input_2:
Enter the array of strings:  cu fcuk code
Output:
[‘cu’]


Input_3:
Enter the array of strings:  blue  green bu
Output:
[ ]


Input_4:
Enter the array of strings:  DeathNote  ote  Death
Output:
[‘ote’, ‘Death’]


Sample Output ?

Executed using terminal Linux

Morae Q!