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. 目前校园百晓生APP与CSDN软件的对比

    不忘初心.注入灵魂 设计之初 在做校园百晓生该APP时,初心是为校园的同学提供便捷的查询校园信息的服务,确实目前的软件已经实现了该功能,但是总感觉缺少一些灵魂,感觉大部分人渴望的重点在于信息而不是自己 ...

  2. [hdu5351]找规律,大整数模板

    题意:f(1)="a",f(2)="b",f(i)=f(i-1)+f(i-2),"+"表示连接符.给定n,m,求f(n)的前m个字符的“ne ...

  3. java 实现仿照微信抢红包算法,实测结果基本和微信吻合,附demo

    实现拼手气红包算法,有以下几个需要注意的地方: 抢红包的期望收益应与先后顺序无关 保证每个用户至少能抢到一个预设的最小金额,人民币红包设置的最小金额一般是0.01元,如果需要发其他货币类型的红包,比如 ...

  4. Nginx|构建简单的文件服务器(mac) 续-FastDFS安装(mac)|文件存储方案

    目录 Nginx|构建简单的文件服务器(mac) 1 所需安装包 2 安装fastdfs-nginx-module-master 3 安装Nginx Nginx|构建简单的文件服务器(mac) 续上文 ...

  5. 安装Kibana出现的问题

    安装Kibana出现的问题 前言 该问题的出现是在安装配置完成之后,也就是说下载好了kibana的相关包,在启动过程中出现的错误,该错误是在centos6的机器上引发的,是因为系统中的GLIBC_2. ...

  6. java ->多线程_线程池

    线程池概念 线程池,其实就是一个容纳多个线程的容器,其中的线程可以反复使用,省去了频繁创建线程对象的操作,无需反复创建线程而消耗过多资源. 我们详细的解释一下为什么要使用线程池?(程序优化) 在jav ...

  7. Tensorflow从0到1(一)之如何安装Tensorflow(Windows和Linux两种版本)

    现在越来越多的人工智能和机器学习以及深度学习,强化学习出现了,然后自己也对这个产生了点兴趣,特别的进行了一点点学习,就通过这篇文章来简单介绍一下,关于如何搭建Tensorflow以及如何进行使用.建议 ...

  8. 汉语拼音转换工具包pypinyin

    #pip install pypinyin汉字转换汉语拼音 from pypinyin import lazy_pinyin,TONE,TONE2,TONE3 str="你知道我是谁吗?&q ...

  9. MySQL索引及优化(1)存储引擎和底层数据结构

    在昨天的面试中问到了MySQL索引怎么优化(查询很慢怎么办),回答的很不理想,所以今天来总结几篇关于MySQL索引的知识. 1.什么是索引? 首先我们一定要明确什么是索引?我自己的总结就是索引是一种数 ...

  10. unity---string.Format()

    string.Format用法 string.Format("{0}{1}{2}",str1,str2,str3) string.Format("{0:D2}{1:D2} ...