-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstringmatching.py
More file actions
36 lines (23 loc) · 1.08 KB
/
stringmatching.py
File metadata and controls
36 lines (23 loc) · 1.08 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# Given an array of string words. Return all strings in words which is substring of another word in any order.
# String words[i] is substring of words[j], if can be obtained removing some characters to left and/or right side of words[j].
#Example:
# Input: words = ["mass","as","hero","superhero"]
# Output: ["as","hero"]
# Explanation: "as" is substring of "mass" and "hero" is substring of "superhero".
# ["hero","as"] is also a valid answer.
#Solution 1
class Solution:
def stringMatching(self, words: List[str]) -> List[str]:
substring=[]
for index_1,words_a in enumerate(words):
for index_2, words_b in enumerate(words):
if(index_1!=index_2 and words_a in words_b):
substring.append(words_a)
break
return substring
#solution 2
lass Solution:
def stringMatching(self, words: List[str]) -> List[str]:
size = len( words )
substr = set( words[i] for i in range(size) for j in range(size) if i != j and words[i] in words[j] )
return [ *substr ]