【Rust—LeetCode题解】1408.数组中的字符串匹配
前言
等级: 🟩
解答: 🦀
题目描述
给你一个字符串数组 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”] 输出:[]
解题思路
-
冒泡排序的思想,左右判断是否满足要求
😺代码😺
impl Solution { pub fn string_matching(words: Vec<String>) -> Vec<String> { use std::collections::HashSet; let mut ans = HashSet::new(); for (i, w) in words.iter().enumerate() { for j in words.iter().skip(i + 1) { if w.contains(j) { ans.insert(j.clone()); } else if j.contains(w) { ans.insert(w.clone()); } } } ans.into_iter().collect::<Vec<String>>() } }
🐯执行结果🐯
🌀更多题解🌀
-
更多力扣题解请到我的仓库:
下一篇:
C语言:二维数组传参的格式(详细+总结)