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
  • 相关阅读:
    面试题整理
    Node.js核心模块-stream流
    Node.js核心模块-crypto加密
    Node.js核心模块-assert
    Node.js全局对象-process
    nodemon
    随笔
    python学习笔记(十四): unittest
    python学习笔记(十三): 多线程多进程
    python学习笔记(十二):发送邮件
  • 原文地址:https://www.cnblogs.com/seyjs/p/12940151.html
Copyright © 2011-2022 走看看