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. angular前端框架简单小案例

    一.angular表达式 <head> <meta charset="UTF-8"> <title>Title</title> &l ...

  2. SSL协议握手工作流程详解(双向HTTPS流程)

    参考学习文档:http://www.cnblogs.com/jifeng/archive/2010/11/30/1891779.html SSL协议的工作流程: 服务器认证阶段: 1)客户端向服务器发 ...

  3. 切片原型[start:stop:step]

    切片操作符在Python中的原型是 [start:stop:step] 步长值:默认是一个接着一个切取,如果为2,则表示进行隔一取一操作.步长值为正时表示从左向右取,如果为负,则表示从右向左取.步长值 ...

  4. GitHub使用SSH连接以及生成修改添加密钥详细过程

    目录 1. 先看看本地有没有SSH密钥 2. 生成/修改密钥 3. 把SSH密钥添加到ssh-agent 4. 把SSH密钥添加到GitHub账户里 5. 测试使用ssh地址clone仓库 6. 把远 ...

  5. vue 事件修饰符(阻止默认行为和事件冒泡)

    1. 原生js中,阻止事件冒泡,获取点击对象, e.stopPropagation(); 2. vue阻止事件冒泡@click.stop="show" <body> & ...

  6. Nodejs模块介绍

    1.模块系统 require:引入模块,返回一个对象 module:指代当前的模块对象 module.exports:当前模块的导出对象 exports:指代module.exports __file ...

  7. Notification API,为你的网页添加桌面通知推送

    Notification 是什么 MDN: Notifications API 的 Notification 接口用于配置和向用户显示桌面通知.这些通知的外观和特定功能因平台而异,但通常它们提供了一种 ...

  8. 在ORACLE中实现SELECT TOP N的方法----[转]

    1.在ORACLE中实现SELECT TOP N 由于ORACLE不支持SELECT TOP语句,所以在ORACLE中经常是用ORDER BY跟ROWNUM的组合来实现SELECT TOP N的查询. ...

  9. 安卓网络编程学习(1)——java原生网络编程(2)

    写在前面 该博客紧接上篇博客:https://www.cnblogs.com/wushenjiang/p/12937531.html,继续学习post请求,带参数的post和get请求以及文件上传与下 ...

  10. Windows系统下Git的下载和配置

    简介:Git是一款免费.开源的分布式版本控制系统,可记录文件每次改动,便于多人协作编辑. 下载:git-for-windows下载地址https://git-for-windows.github.io ...