# LeetCode | 1408. String Matching in an Array数组中的字符串匹配【Python】

LeetCode 1408. String Matching in an Array数组中的字符串匹配【Easy】【Python】【字符串】

### Problem

LeetCode

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 1:

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.


Example 2:

Input: words = ["leetcode","et","code"]
Output: ["et","code"]
Explanation: "et", "code" are substring of "leetcode".


Example 3:

Input: words = ["blue","green","bu"]
Output: []


Constraints:

• 1 <= words.length <= 100
• 1 <= words[i].length <= 30
• words[i] contains only lowercase English letters.
• It’s guaranteed that words[i] will be unique.

### 问题

输入：words = ["mass","as","hero","superhero"]

["hero","as"] 也是有效的答案。


输入：words = ["leetcode","et","code"]



输入：words = ["blue","green","bu"]



• 1 <= words.length <= 100
• 1 <= words[i].length <= 30
• words[i] 仅包含小写英文字母。
• 题目数据 保证 每个 words[i] 都是独一无二的。

### 思路

暴力，最后要去重。


##### Python3代码
from typing import List
import copy

class Solution:
def stringMatching(self, words: List[str]) -> List[str]:
n = len(words)
res = []
for i in range(n):
temp = copy.deepcopy(words)
temp.remove(words[i])

for j in range(n - 1):
if words[i] in temp[j]:
res.append(words[i])
return list(set(res))


### GitHub链接

Python

04-12 327 05-30 7324
07-04 9424
08-09 1万+
12-08 1万+
07-22 1万+
03-29 642
08-06 5251
01-25 3万+
12-12 1万+