当前位置:网站首页>LeetCode 1408. String matching in arrays
LeetCode 1408. String matching in arrays
2022-08-07 06:22:00 【Sasakihaise_】
1408. String matching in an array


[Violence] The amount of data is not large, so it is directly violent. Contains in java can be used to determine whether a string contains another string, but it should be noted that the final answer cannot contain repeated strings.
class Solution {// violence 3:20 1public List stringMatching(String[] words) {List ans = new ArrayList();int i, j, n = words.length;for (i = 0; i < n; i++) {for (j = 0; j < n; j++) {if (i != j && words[j].contains(words[i])) {ans.add(words[i]); break;}}}return ans;}} class Solution {public:// violence 3:23 2vector stringMatching(vector& words) {int i, j, n= words.size();vector ans;for (i = 0; i < n; i++) {for (j = 0; j < n; j++) {if (i != j && words[j].find(words[i]) != -1) {ans.push_back(words[i]); break;}}}return ans;}}; [Hash + Split String] My own method, but slow
class Solution {// hash table 12:49public List stringMatching(String[] words) {Set set = new HashSet();for (var str: words) set.add(str);List ans = new ArrayList();for (var st: words) {String str = st;int n = str.length();for (var i = 0; i < n; i++) {for (var j = i + 1; j <= n; j++) {String tmp = str.substring(i, j);if (!tmp.equals(str) && set.contains(tmp)) {ans.add(tmp);set.remove(tmp);}}}}return ans;}} 边栏推荐
猜你喜欢
随机推荐
dp,dpi,px知识补充
C语言力扣第60题之排列序列。广度优先搜索、简单除法定位
Qt uses SQLite's performance-optimized billion-point record
关于面试
RuoYi学习笔记
dagre/dagre-d3绘制流程图
TRACE32 - Memory Fill Test Data.Pattern
servlet tutorial 1: environment setup and new servlet project
案例:搭建Zabbix监控系统
路由交换综合实验
2022A特种设备相关管理(电梯)特种作业证考试题库模拟考试平台操作
机器学习之信用卡欺诈识别(严重类失衡数据建模)
Nacos集群搭建(图文教程)
OS模块中获取当前文件的绝对路径的相关方法
LeetCode 1408. 数组中的字符串匹配
Scrapy抓取新浪微博
The regular expression of the shell and the Three Musketeers grep command
数组扁平化
2020年初全国行政区划矢量数据
支付宝支付








