给一个单词一个字典,每次删除单词里任一个字母直到剩下一个字母,形成一个序列,比如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. xml提取

    $url = 'http://221.232.141.108/hsdcw/news.xml'; $opts = array( 'http'=>array( 'method'=>" ...

  2. PHP5 mysqli 教程

    mysqli提供了面向对象和面向过程两种方式来与数据库交互,分别看一下这两种方式. 1.面向对象 在面向对象的方式中,mysqli被封装成一个类,它的构造方法如下: __construct ([ st ...

  3. mysql 密码篇

    通过MySQL命令行,可以修改MySQL数据库的密码,下面就为您详细介绍该MySQL命令行,如果您感兴趣的话,不妨一看. 格式:mysqladmin -u用户名 -p旧密码 password 新密码 ...

  4. Blender to XPS(blender 2.7x Internal materials)

    Things we are gonna need are Blender 2.7x www.blender.org/ XPS tools addon for Blender A model made ...

  5. FW nexus docker

    原文地址: http://www.cnblogs.com/wzy5223/p/5410990.html Nexus 3.0 可以创建三种docker仓库: 1. docker (proxy)     ...

  6. 变长数组列表ArrayList

    简介:此数据结构定义为一个ArrayList结构体类型,维护了一个内部堆数组.通过realloc函数实现了数组容量自动扩充,每次扩充到原来的2倍. 通过函数指针实现了使用者根据自己的需求按条件按查找目 ...

  7. acl 是一个跨平台的网络通信库及服务器编程框架

    acl 工程是一个跨平台(支持LINUX,WIN32,Solaris,MacOS,FreeBSD)的网络通信库及服务器编程框架,同时提供更多的实用功能库.通过该库,用户可以非常容易地编写支持多种模式( ...

  8. C 不改变顺序,原址剔除数组中的0元素

    #include <stdio.h> #include <malloc.h> #include <stdlib.h> #include <time.h> ...

  9. php--如何解决网站分页导致的SEO问题

    如何解决网站分页导致的SEO问题 分页(pagination)是一种自动分页机制,可以将移动Web窗体中的内容分割成一组组较小的页进行呈现,以适合于特定的设备,该机制还呈现可用于浏览到其他页的用户界面 ...

  10. NodeJS模块的使用

    在NodeJS中,每个js文件就是一个模块,而文件路径就是模块名, 在编写每个模块时,都有require.exports.module三个预先定义好的变量可供使用. require函数用于在当前模块中 ...