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 ,数组中的每个字符串都可以看作是一个单词。请你按 任意 顺序返回 words 中是其他单词的子字符串的所有单词。

如果你可以删除 words[j] 最左侧和/或最右侧的若干字符得到 word[i] ,那么字符串 words[i] 就是 words[j] 的一个子字符串。

示例 1:

输入:words = ["mass","as","hero","superhero"]
输出:["as","hero"]
解释:"as" 是 "mass" 的子字符串,"hero" 是 "superhero" 的子字符串。
["hero","as"] 也是有效的答案。

示例 2:

输入:words = ["leetcode","et","code"]
输出:["et","code"]
解释:"et" 和 "code" 都是 "leetcode" 的子字符串。

示例 3:

输入:words = ["blue","green","bu"]
输出:[]

提示:

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

思路

字符串

暴力,最后要去重。

时间复杂度: O(n^2)

空间复杂度: O(n)

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

原文地址:https://www.cnblogs.com/wonz/p/12687765.html

时间: 2024-12-29 16:23:52

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

leetcode 第184场周赛第一题(数组中的字符串匹配)

一.函数的运用 1,strstr(a,b); 判断b是否为a的子串,如果是,返回从b的开头开始到a的结尾 如“abcdefgh” “de” 返回“defgh”: 如果不是子串,返回NULL: 2,memcpy(a,b+n,c); 将b串从第n位后的c个字符串复制到a中,返回a串: (注:做完函数后需要添加上b[c] = '\0') 如果吧b+n换成b就是从头开始 将代码换成 memcpy(arr[cnt], words[i], strlen(words[i])+1); 就是直接复制words[i

[LeetCode] Kth Largest Element in an Array 数组中第k大的数字

Find the kth largest element in an unsorted array. Note that it is the kth largest element in the sorted order, not the kth distinct element. For example, Given [3,2,1,5,6,4] and k = 2, return 5. Note: You may assume k is always valid, 1 ≤ k ≤ array'

20.4.12 周赛 数组中的字符串匹配 简单

题目 给你一个字符串数组 words ,数组中的每个字符串都可以看作是一个单词.请你按 任意 顺序返回 words 中是其他单词的子字符串的所有单词. 如果你可以删除 words[j] 最左侧和/或最右侧的若干字符得到 word[i] ,那么字符串 words[i] 就是 words[j] 的一个子字符串. 示例 1: 输入:words = ["mass","as","hero","superhero"] 输出:["

LeetCode | 0215. Kth Largest Element in an Array数组中的第K个最大元素【Python】

LeetCode 0215. Kth Largest Element in an Array数组中的第K个最大元素[Medium][Python][快排][堆] Problem LeetCode Find the kth largest element in an unsorted array. Note that it is the kth largest element in the sorted order, not the kth distinct element. Example 1:

将数组中的字符按出现次数多少排序输出

原题 一个有N个元素的集合,其中有相同元素. 需要得到按重复元素多少排序的新集合. 输入  {"a","b","c","c","a","c"} 输出  {"c","a","b"} 求算法 import java.util.ArrayList; import java.util.Collections; import java.u

倒置字符数组中的字符

问题:如何把字符串 "We are the world" 转成 "world the are we" ?如果最多只能用两个额外的变量又该如何实现这样的转换? 分析:1.把字符串转成字符数组,然后对字符数组进行操作. 2.选择倒置字符数组的方法:用一个临时变量temp来交换两个字符,然后依次移动数组中的其他元素:利用 A=A+B, B=A-B, A=A-B 方式来交换两个字符. 3.根据数组中首尾字符的位置来倒置它们之间的字符. 4.优化方法:减少循环的次数,减少变量

LeetCode 34 Search for a Range (有序数组中查找给定数字的起止下标)

题目链接: https://leetcode.com/problems/search-for-a-range/?tab=Description Problem: 在已知递减排序的数组中,查找到给定数字的起止下标 采用两遍扫描: 第一遍扫描得到给定数字的起始下标,(从下标i==0开始到nums.lenght-1) 第二遍扫描从第一遍扫描得到的下标开始进行扫描 参考代码: package leetcode_50; /*** * * @author pengfei_zheng * 数组中找到targe

LeetCode&mdash;&mdash;Single Number II(找出数组中只出现一次的数2)

问题: Given an array of integers, every element appears three times except for one. Find that single one. Note: Your algorithm should have a linear runtime complexity. Could you implement it without using extra memory?   Single Number I 升级版,一个数组中其它数出现了

215 Kth Largest Element in an Array 数组中的第K个最大元素

在未排序的数组中找到第 k 个最大的元素.请注意,它是数组有序排列后的第 k 个最大元素,而不是第 k 个不同元素.例如,给出 [3,2,1,5,6,4] 和 k = 2,返回 5.注意事项:你可以假设 k 总是有效的,1 ≤ k ≤ 数组的长度. 详见:https://leetcode.com/problems/kth-largest-element-in-an-array/description/ 方法一: class Solution { public: int findKthLarges