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 ...
随机推荐
- php-->mongodb[curd操作]
<?php /** * PHP操作MongoDB学习笔记 */ //************************* //** 连接MongoDB数据库 **// //********* ...
- Amazon captcha
Navigated to https://images-na.ssl-images-amazon.com/captcha/xzqdsmvh/Captcha_dxnamcsjjf.jpgdocument ...
- html EL表达式抬头
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%> <c:forE ...
- ajax+jsp自动刷新
通过 AJAX,JavaScript 可使用 JavaScript 的 XMLHttpRequest 对象来直接与服务器进行通信.通过这个对象, JavaScript 可在不重载页面的情况与 Web ...
- postgre去重复记录
postgre去重复记录,主要用到row定位的一个系统表示 “ctid”,能查出纯净的不重复的记录,那要删掉重复值也就容易了,自己去折腾吧. 我所涉及的是得到不重复的记录,就一句话: select c ...
- (转)投票系统,更改ip刷票
前言 相信大家平时肯定会收到朋友发来的链接,打开一看,哦,需要投票.投完票后弹出一个页面(恭喜您,您已经投票成功),再次点击的时候发现,啊哈,您的IP(***.***.***.***)已经投过票了,不 ...
- 一段检测IP设备是否在线的代码
原理是通过发送ARP包来检测 uses WinSock function SendARP(const DestIP, SrcIP: Cardinal; pMacAddr: PULONG; var Ph ...
- ubuntu navicat
接下来是从网络上下载Chrome对应是版本的包,小编的系统是64位的,因此,执行:wget https://dl.google.com/linux/direct/google-chrome-stabl ...
- zepto源码--几个判断函数--学习笔记
几个需要经常用到的类型判断: 自定义一个类似于typeof的函数,提供更多的类型判断. class2type[toString.call(obj)] 是对class2type的取值 在后面通过循环对c ...
- 设计模式:享元模式(Flyweight)
定 义:运用共享技术有效地支持大量细粒度的对象. 结构图: 内部状态:在享元对象内部并且不会随环境而改变的共享部分. 外部状态:随环境改变而改变的.不可共享的状态. Flyweight类,具体享元 ...