472. 连接词

给定一个不含重复单词的列表,编写一个程序,返回给定单词列表中所有的连接词。

连接词的定义为:一个字符串完全是由至少两个给定数组中的单词组成的。

示例:

输入: [“cat”,“cats”,“catsdogcats”,“dog”,“dogcatsdog”,“hippopotamuses”,“rat”,“ratcatdogcat”]

输出: [“catsdogcats”,“dogcatsdog”,“ratcatdogcat”]

解释: “catsdogcats"由"cats”, “dog” 和 "cats"组成;

“dogcatsdog"由"dog”, "cats"和"dog"组成;

“ratcatdogcat"由"rat”, “cat”, "dog"和"cat"组成。

说明:

给定数组的元素总数不超过 10000。

给定数组中元素的长度总和不超过 600000。

所有输入字符串只包含小写字母。

不需要考虑答案输出的顺序。

class Solution {
private Set<String> wordSet; public List<String> findAllConcatenatedWordsInADict(String[] words) { wordSet = new HashSet<>();
for (String word : words) {
wordSet.add(word);
} List<String> result = new ArrayList<String>();
Arrays.asList(words).parallelStream().forEach(word -> {
if (dfs(word, 0, 0, new StringBuilder())) {
result.add(word);
}
}); return result;
} private boolean dfs(String word, int count, int start, StringBuilder sb) {
for (int i = start; i < word.length(); i++) {
sb.append(word.charAt(i));
boolean isWord = wordSet.contains(sb.toString());
if (isWord && dfs(word, count + 1, i + 1, new StringBuilder())) {
return true;
}
}
return count > 1 && (wordSet.contains(sb.toString()) || start == word.length());
}
}

Java实现 LeetCode 472 连接词的更多相关文章

  1. Leetcode 472.连接词

    连接词 给定一个不含重复单词的列表,编写一个程序,返回给定单词列表中所有的连接词. 连接词的定义为:一个字符串完全是由至少两个给定数组中的单词组成的. 示例: 输入: ["cat" ...

  2. [Swift]LeetCode472. 连接词 | Concatenated Words

    Given a list of words (without duplicates), please write a program that returns all concatenated wor ...

  3. Java和Android Http连接程序:使用java.net.URL 下载服务器图片到客户端

    Java和Android Http连接程序:使用java.net.URL 下载服务器图片到客户端 本博客前面博文中利用org.apache.http包中API进行Android客户端HTTP连接的例子 ...

  4. Java for LeetCode 216 Combination Sum III

    Find all possible combinations of k numbers that add up to a number n, given that only numbers from ...

  5. Java for LeetCode 214 Shortest Palindrome

    Given a string S, you are allowed to convert it to a palindrome by adding characters in front of it. ...

  6. Java for LeetCode 212 Word Search II

    Given a 2D board and a list of words from the dictionary, find all words in the board. Each word mus ...

  7. Java for LeetCode 211 Add and Search Word - Data structure design

    Design a data structure that supports the following two operations: void addWord(word)bool search(wo ...

  8. Java for LeetCode 210 Course Schedule II

    There are a total of n courses you have to take, labeled from 0 to n - 1. Some courses may have prer ...

  9. Java for LeetCode 200 Number of Islands

    Given a 2d grid map of '1's (land) and '0's (water), count the number of islands. An island is surro ...

随机推荐

  1. 一文教你如何在ubuntu上快速搭建STM32 CubeIDE环境(图文超详细+文末有附件)

    在快速ubuntu上安装cubeide你值得拥有:适合对linux系统还不是很熟悉的同学: 文章目录 1 下载 cubeide 2 找到软件 3 安装 4 附件 5 总结 1 下载 cubeide 登 ...

  2. FOC中电流环调试的宝贵经验总结(有理有据+全盘拖出)

    你是否经历过一个人独自摸索前进磕磕碰碰最终体无完肤,然后将胜利的旗帜插到山顶的时刻,如果有,本文也许能帮你在调试FOC电流环的时候给你带来一些帮助和思路. 如果本文帮到了您,请帮忙点个赞

  3. 配置类为什么要添加@Configuration注解呢?

    配置类为什么要添加@Configuration注解呢? 本系列文章: 读源码,我们可以从第一行读起 你知道Spring是怎么解析配置类的吗? 推荐阅读: Spring官网阅读 | 总结篇 Spring ...

  4. jconsole+idea监控+(jvisualvm 本地内存分析)

    1.idea启动配置 添加以下内容 -Djava.rmi.server.hostname=127.0.0.1 -Dcom.sun.management.jmxremote -Dcom.sun.mana ...

  5. HttpRequestUtils post get请求

    package com.nextjoy.projects.usercenter.util.http; /** * Created by Administrator on 2016/10/20. */ ...

  6. Blazor WebAssembly 船新项目下载量测试 , 仅供参考.

    前言: 昨天 Blazor WebAssembly 3.2 正式发布了.  更新 VS2019后就能直接使用. 新建了两个PWA项目,  一个不用asp.net core (静态部署), 一个使用as ...

  7. Java 面向对象基础

    面向对象的基础 局部变量和成员变量区别: 1)定义的位置不同    成员变量直接定义在class中    局部变量在某个{}中或者再某个方法中 2)在内存中的位置不同    对象的成员变量会在内存中的 ...

  8. Oracle 中同义词使用

    一.数据库对象: 模式对象: 数据库对象是逻辑结构的集合,最基本的数据库对象是表; 其他对象包括:create增.drop删.改alter 同义词.序列.视图.索引 1.同义词: ①. 现有对象的一个 ...

  9. 3D视觉基础(基本原理及3D传感器基本参数)

    本人所在行业属于3D视觉方向,因此最近也是学习了很多3D视觉的知识,这次专门总结一下. 传统工业机器视觉中,2D指的是X方向加Y方向,那么3D视觉自然就是加了一个Z方向.目前我接触到的公司产品是3D激 ...

  10. mysql Invalid use of group function的解决办法

    错误语句:SELECT s.SID, s.Sname, AVG(a.score)FROM student sLEFT JOIN sc a ON s.SID = a.SID WHERE AVG(a.sc ...