• 【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
  • 相关阅读:
    while循环和递归
    ASP.NET Core Swagger 显示接口注释
    ABP依赖注入
    EF Core 笔记
    .NET Core 管道过滤器扩展
    ABP权限认证
    552 you must authentication
    C# 多线程写文件,时常写不成功
    .Net 的一些插件
    多线程下HttpContext.Current 的问题
  • 原文地址:https://www.cnblogs.com/seyjs/p/12940151.html
Copyright © 2020-2023  润新知