zoukankan      html  css  js  c++  java
  • 【leetcode】1408. String Matching in an Array

    题目如下:

    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.

    解题思路:最多才100个单词,每个判断一遍就好了。

    代码如下:

    class Solution(object):
        def stringMatching(self, words):
            """
            :type words: List[str]
            :rtype: List[str]
            """
            res = []
            for i in range(len(words)):
                flag = False
                for j in range(len(words)):
                    if i == j:continue
                    elif words[i] in words[j]:
                        flag = True
                        break
                if flag:res.append(words[i])
            return res
  • 相关阅读:
    避免多次提交
    Django 10
    Django 08
    Django 07
    Django 06
    Django 05
    Django 04
    Django 03
    Django 02
    Django 01
  • 原文地址:https://www.cnblogs.com/seyjs/p/12940151.html
Copyright © 2011-2022 走看看