关于后端:1408-数组中的字符串匹配-简单模拟题

0次阅读

共计 1340 个字符,预计需要花费 4 分钟才能阅读完成。

题目形容

这是 LeetCode 上的 1408. 数组中的字符串匹配 ,难度为 简略

Tag :「模仿」

给你一个字符串数组 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] 都是举世无双的。

模仿

依据题意进行模仿即可。

Java 代码:

class Solution {public List<String> stringMatching(String[] ss) {List<String> ans = new ArrayList<>();
        int n = ss.length;
        for (int i = 0; i < n; i++) {for (int j = 0; j < n; j++) {if (i == j) continue;
                if (ss[j].indexOf(ss[i]) >= 0) {ans.add(ss[i]);
                    break;
                }
            }
        }
        return ans;
    }
}

TypeScript 代码:

function stringMatching(ss: string[]): string[] {const ans = new Array<string>()
    const n = ss.length
    for (let i = 0; i < n; i++) {for (let j = 0; j < n; j++) {if (i == j) continue
            if (ss[j].indexOf(ss[i]) >= 0) {ans.push(ss[i])
                break
            }
        }
    }
    return ans
};
  • 工夫复杂度:$O(n^2 \times m^2)$
  • 空间复杂度:$O(1)$

最初

这是咱们「刷穿 LeetCode」系列文章的第 No.1408 篇,系列开始于 2021/01/01,截止于起始日 LeetCode 上共有 1916 道题目,局部是有锁题,咱们将先把所有不带锁的题目刷完。

在这个系列文章外面,除了解说解题思路以外,还会尽可能给出最为简洁的代码。如果波及通解还会相应的代码模板。

为了不便各位同学可能电脑上进行调试和提交代码,我建设了相干的仓库:https://github.com/SharingSou…。

在仓库地址里,你能够看到系列文章的题解链接、系列文章的相应代码、LeetCode 原题链接和其余优选题解。

更多更全更热门的「口试 / 面试」相干材料可拜访排版精美的 合集新基地 🎉🎉

本文由 mdnice 多平台公布

正文完
 0