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. OpenCV 经纬法将鱼眼图像展开

    文章目录 前言 理论部分 鱼眼展开流程 鱼眼标准坐标计算 标准坐标系与球坐标的转换 代码实现 测试效果如下图 总结 this demo on github 前言 鱼眼镜头相比传统的镜头,视角更广,采集 ...

  2. FZU2105 线段树 (按位操作)

    题目: Given N integers A={A[0],A[1],...,A[N-1]}. Here we have some operations: (元素和操作元素 < 16) Opera ...

  3. 使用 pyautogui 进行跨平台的 GUI 自动化操作

    有个朋友最近问我有没有推荐 GUI 桌面应用自动化的技术,我只能回答他:不好意思,这个真有,他是 pyautogui.主要有三大特征: 纯纯的 python, 源码一览无余: 跨平台,linux, w ...

  4. Mybatis学习笔记汇总(包括源码和jar包)

    博客整理 Mybatis学习笔记(一)--对原生jdbc中问题的总结 Mybatis学习笔记(二)--Mybatis框架 Mybatis学习笔记(三)--入门程序 MyBatis学习笔记(四)--入门 ...

  5. linux下安装gmp遇到 configure:error:no usable m4 in$path or /user/5bin解决方案

    安装过程中遇到如下报错: 上面的报错是因为你没有安装m4,安装m4就可以了:以下两种命令人选其一: #yum install m4 或 #apt-get install m4 ps:如果遇到权限问题就 ...

  6. wepy 小程序开发(interceptor拦截器 && WXS)

    WePY全局拦截器可对原生API的请求进行拦截. import wepy from 'wepy'; export default class extends wepy.app { constructo ...

  7. reids不重启切换rdb到aof

    reids不重启切换rdb到aof

  8. 在centos8使用Docker部署Django项目

    引言 在本文中将介绍在Docker中通过django + uwsgi + nginx部署方式部署Django项目, 由于记录的是学习过程,使用的都是目前较高的版本. python 版本为3.8.3 d ...

  9. PAT-1134 Vertex Cover (图的建立 + set容器)

    A vertex cover of a graph is a set of vertices such that each edge of the graph is incident to at le ...

  10. 四、$jQuery

    1.你觉得jQuery或zepto源码有哪些写的好的地方 jquery源码封装在一个匿名函数的自执行环境中,有助于防止变量的全局污染,然后通过传入window对象参数,可以使window对象作为局部变 ...