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 ...
随机推荐
- 五一结束,北戴河,yy,差一点,不太敢
collectionView Cell 设置颜色,蓝色,但是其他cell颜色也蓝色了,因为只写了if 没写else if (indexPath.item == 0) { cell.background ...
- P1351 联合权值
#include <bits/stdc++.h> using namespace std; const int maxn = 200005; vector<int> son[m ...
- delphi下如何获得不带扩展名的文件名?
Edit1.Text:=ChangeFileExt(ExtractFileName(Application.ExeName),'') ; //获取到应用程序名后,将后缀名清空就可以啦.
- [教程] 以本论坛为例,手把手教你使用按键精灵POST登陆网页
本帖最后由 isaacc 于 2012-2-26 11:08 编辑 整个操作,很无脑.只要你够勤快,你学不会,你来咬我.懒人和伸手党就直接复制代码去玩吧,但我不是叫你拿去干坏事. 准备工具:WPE和I ...
- 怎么样用opencv将彩色图片转化成像素值只有0和255的灰度图?
分类: OpenCV [Q1]怎么样用opencv将彩色图片转化成像素值只有0和255的灰度图? 进行灰度化,IplImage* pImg = cvLoadImage( "C:\\1.b ...
- 帝国CMS备忘
一. 2级导航: 类似下图这种导航: 实现方式如: 1. 定义一个标签模板,记住ID,具体内容如: 页面模板内容: <li><a href=”[!—bclassurl—]”>[ ...
- 20145211 《Java程序设计》第2周学习总结——桃花依旧笑春风
教材学习内容总结 基本类型 整数 short 2字节,int 4字节,long 8字节 字节 byte 1字节 浮点数 float 4字节,double 8字节 字符 char 2字节(包括字母.汉字 ...
- Selenium2学习-016-WebUI自动化实战实例-014-Selenium 窗口选择
在日常的 WebUI 自动化测试脚本编写过程中,经常需要打开新的页面,或者在多个打开的页面之间进行切换,以对页面元素进行相应的操作,以模拟用户的行为,实现 UI 的自动化测试.在过往的时间中,经常有初 ...
- php中的编码问题
转自:http://www.jb51.net/article/22501.htm php的header来定义一个php页面为utf编码或GBK编码 php页面为utf编码 header("C ...
- 《Maven实战》阅读笔记
java -versionmvn -vmvn help:system m2eclipse maven->install MAVEN_OPTS: -Xms128m -Xmx512mmvn clea ...