给一个单词一个字典,每次删除单词里任一个字母直到剩下一个字母,形成一个序列,比如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的更多相关文章

  1. Leetcode211. Add and Search Word - Data structure design 添加与搜索单词 - 数据结构设计

    设计一个支持以下两种操作的数据结构: void addWord(word) bool search(word) search(word) 可以搜索文字或正则表达式字符串,字符串只包含字母 . 或 a- ...

  2. 【LeetCode】211. Add and Search Word - Data structure design 添加与搜索单词 - 数据结构设计

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 公众号:负雪明烛 本文关键词:Leetcode, 力扣,211,搜索单词,前缀树,字典树 ...

  3. 【刷题-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 ...

  4. [LeetCode] Add and Search Word - Data structure design 添加和查找单词-数据结构设计

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

  5. Add and Search Word

    Trie 树的一个应用 Design a data structure that supports the following two operations: void addWord(word) b ...

  6. 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 ...

  7. [LintCode] Add and Search Word 添加和查找单词

    Design a data structure that supports the following two operations: addWord(word) and search(word) s ...

  8. (*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 ...

  9. Add and Search Word - Data structure design

    https://leetcode.com/problems/add-and-search-word-data-structure-design/ Design a data structure tha ...

随机推荐

  1. jquery()的三种$()

    jQuery中的$以及选择器总结 $号是jQuery”类”的一个别称,$()构造了一个jQuery对象.所以,”$()”可以看作jQuery的”构造函数”(个人观点). 一.$符号 1.$()可以是$ ...

  2. 为什么数值类型byte取值范围是(-128~127)?

    在解决这个问题之前,我们先了解几个概念? 一.原码, 反码, 补码的概念 正数的反码和补码都与原码一样: 负数的反码.补码与原码不同,负数的反码:原码中除去符号位,其他的数值位取反,0变1,1变0.负 ...

  3. visual studio 2005 编fortran程序,运行后dos窗口显示问题

    比如程序: program main implicit none write(*,*) "AAAAAAAAAAAAAAAAAAAAAAAA" stop end 虽然可以看见DOS窗 ...

  4. Ubuntu/Deepin下常用软件汇总(持续更新)

    最近开始用Ubuntu了,好多软件都不是常用的了,在这边留底,以免忘记.如果没有写安装方法,则直接在软件源中可以找到 UNetbootin U盘制作工具,制作Ubuntu的安装U盘超好用 Braser ...

  5. Apache Camel

    Apache Camel 1 import org.apache.camel.CamelContext; import org.apache.camel.builder.RouteBuilder; i ...

  6. java event

    What is an Event? Change in the state of an object is known as event i.e. event describes the change ...

  7. php 请求参数限制

    公司有个群发短信的小项目,项目上线了很久也没有什么问题,最近有商家说 我短信群发不能用 现象是:发现有时候可以发送,有时候不可以发送,看截图发送的手机数量不一样 通过调试php代码发现 php 只接受 ...

  8. Windows下mysql自动备份的最佳方案

    网上有很多关于window下Mysql自动备份的方法,其实不乏一些不好的地方和问题,现总结出一个最好的方法供大家参考: 新建一个记事本,然后重命名为: mysql_backup.bat 然后单击右键选 ...

  9. DOM、SAX、JDOM、DOM4J四种XML解析方法PK

    基础方法(指不需要导入jar包,java自身提供的解析方式):DOM.SAXDOM:是一种平台无关的官方解析方式   --优点:          (1)形成了树结构,直观好理解,代码更易编写     ...

  10. js中将字符串转换成json的三种方式

    1,eval方式解析,恐怕这是最早的解析方式了.如下: function strToJson(str){ var json = eval('(' + str + ')'); return json; ...