G面经Prepare: Search word delete sequence in dictionary
给一个单词一个字典,每次删除单词里任一个字母直到剩下一个字母,形成一个序列,比如office->offce->ofce->ofc->oc->c。问是否字典里存在一个这种序列
package checkDictExistSequence;
import java.util.*; public class Solution {
HashSet<String> dict = new HashSet<String>(); public String check(String[] arr, String word) {
for (String str : arr)
dict.add(str);
if (checkSeq(new StringBuffer(word))) return "true";
return "false";
} public boolean checkSeq(StringBuffer sb) {
if (sb.length() == 1) return true;
for (int i=0; i<=sb.length()-1; i++) {
char cur = sb.charAt(i);
sb.deleteCharAt(i);
if (dict.contains(sb.toString())) {
if (checkSeq(sb)) return true;
}
sb.insert(i, cur);
}
return false;
} /**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
Solution sol = new Solution();
System.out.println(sol.check(new String[]{"offce", "ofce", "ofc", "oc", "c"}, "office"));
} }
G面经Prepare: Search word delete sequence in dictionary的更多相关文章
- Leetcode211. Add and Search Word - Data structure design 添加与搜索单词 - 数据结构设计
设计一个支持以下两种操作的数据结构: void addWord(word) bool search(word) search(word) 可以搜索文字或正则表达式字符串,字符串只包含字母 . 或 a- ...
- 【LeetCode】211. Add and Search Word - Data structure design 添加与搜索单词 - 数据结构设计
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 公众号:负雪明烛 本文关键词:Leetcode, 力扣,211,搜索单词,前缀树,字典树 ...
- 【刷题-LeetCode】211. Add and Search Word - Data structure design
Add and Search Word - Data structure design Design a data structure that supports the following two ...
- [LeetCode] Add and Search Word - Data structure design 添加和查找单词-数据结构设计
Design a data structure that supports the following two operations: void addWord(word) bool search(w ...
- Add and Search Word
Trie 树的一个应用 Design a data structure that supports the following two operations: void addWord(word) b ...
- 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 ...
- [LintCode] Add and Search Word 添加和查找单词
Design a data structure that supports the following two operations: addWord(word) and search(word) s ...
- (*medium)LeetCode 211.Add and Search Word - Data structure design
Design a data structure that supports the following two operations: void addWord(word) bool search(w ...
- Add and Search Word - Data structure design
https://leetcode.com/problems/add-and-search-word-data-structure-design/ Design a data structure tha ...
随机推荐
- hiho42 : 骨牌覆盖问题·二
描述 上一周我们研究了2xN的骨牌问题,这一周我们不妨加大一下难度,研究一下3xN的骨牌问题?所以我们的题目是:对于3xN的棋盘,使用1x2的骨牌去覆盖一共有多少种不同的覆盖方法呢?首先我们可以肯定, ...
- Apche Kafka 的生与死 – failover 机制详解
Kafka 作为 high throughput 的消息中间件,以其性能,简单和稳定性,成为当前实时流处理框架中的主流的基础组件. 当然在使用 Kafka 中也碰到不少问题,尤其是 failover ...
- 僵尸传染bfs
#include<stdio.h> int map[4][4]={0,0,0,1, 0,0,1,1, 0,0,1,0, 0,1,0,0}; int mx ...
- PureBasic 集成Form设计器的使用
The PureBasic IDE has a very powerful integrated form designer, which allows to design easily window ...
- java 强制转换
在java中强制类型转换分为基本数据类型和引用数据类型两种,这里我们讨论的后者,也就是引用数据类型的强制类型转换. 在Java中由于继承和向上转型,子类可以非常自然地转换成父类,但是父类转换成子类则需 ...
- 【转】android UI进阶之自定义组合控件
[源地址]http://blog.csdn.net/notice520/article/details/6667827 好久没写博客了.实在是忙不过来,不过再不总结总结真的不行了.慢慢来吧,有好多需要 ...
- linux下线程调用sleep,进程挂起
http://blog.csdn.net/horstlinux/article/details/7911457 http://blog.csdn.net/eroswang/article/detail ...
- JMX初体验
这些天在看<How Tomcat Works>这本书.里面讲到了JMX的内容.对我来说是个新知识点. JMX--Java Management Extensions,即Java管理扩展,是 ...
- Http响应code
Http响应报文 HTTP响应也由三个部分组成,分别是:状态行.消息报头.响应正文. 其中,HTTP-Version表示服务器HTTP协议的版本:Status-Code表示服务器发回的响应状态代码:R ...
- Java实验五报告——TCP传输及加解密
一.实验内容 1.运行教材上TCP代码,结对进行,一人服务器,一人客户端: 2.利用加解密代码包,编译运行代码,一人加密,一人解密: 3.集成代码,一人加密后通过TCP发送: 注:加密使用AES或者D ...