G面经Prepare: Search word delete sequence in dictionary

给一个单词一个字典,每次删除单词里任一个字母直到剩下一个字母,形成一个序列,比如office->offce->ofce->ofc->oc->c。问是否字典里存在一个这种序列
 1 package checkDictExistSequence;
 2 import java.util.*;
 3
 4 public class Solution {
 5     HashSet<String> dict = new HashSet<String>();
 6
 7     public String check(String[] arr, String word) {
 8         for (String str : arr)
 9             dict.add(str);
10         if (checkSeq(new StringBuffer(word))) return "true";
11         return "false";
12     }
13
14     public boolean checkSeq(StringBuffer sb) {
15         if (sb.length() == 1) return true;
16         for (int i=0; i<=sb.length()-1; i++) {
17             char cur = sb.charAt(i);
18             sb.deleteCharAt(i);
19             if (dict.contains(sb.toString())) {
20                 if (checkSeq(sb)) return true;
21             }
22             sb.insert(i, cur);
23         }
24         return false;
25     }
26
27
28     /**
29      * @param args
30      */
31     public static void main(String[] args) {
32         // TODO Auto-generated method stub
33         Solution sol = new Solution();
34         System.out.println(sol.check(new String[]{"offce", "ofce", "ofc", "oc", "c"}, "office"));
35     }
36
37 }
时间: 2024-10-28 11:47:19

G面经Prepare: Search word delete sequence in dictionary的相关文章

[LeetCode] 211. Add and Search Word - Data structure design Java

题目: Design a data structure that supports the following two operations: void addWord(word) bool search(word) search(word) can search a literal word or a regular expression string containing only letters a-z or .. A . means it can represent any one le

LeetCode OJ:Add and Search Word - Data structure design(增加以及搜索单词)

Design a data structure that supports the following two operations: void addWord(word) bool search(word) search(word) can search a literal word or a regular expression string containing only letters a-z or .. A . means it can represent any one letter

[leedcode 211] Add and Search Word - Data structure design

Design a data structure that supports the following two operations: void addWord(word) bool search(word) search(word) can search a literal word or a regular expression string containing only letters a-z or .. A . means it can represent any one letter

(Data structure)Implement Trie And Add and Search Word

Implement Trie (Prefix Tree) Implement a trie with insert, search, and startsWith methods. Note:You may assume that all inputs are consist of lowercase letters a-z solution: class TrieNode { // Initialize your data structure here. boolean isEnd; Trie

[leetcode trie]211. Add and Search Word - Data structure design

Design a data structure that supports the following two operations: void addWord(word) bool search(word) search(word) can search a literal word or a regular expression string containing only letters a-z or .. A . means it can represent any one letter

LeetCode-Add and Search Word

Design a data structure that supports the following two operations: void addWord(word) bool search(word) search(word) can search a literal word or a regular expression string containing only letters a-z or .. A . means it can represent any one letter

Add and Search Word

1 public class WordDictionary { 2 public class TrieNode { 3 TrieNode[] children = new TrieNode[26]; 4 boolean isWord; 5 TrieNode(){} 6 } 7 8 private TrieNode root; 9 10 public WordDictionary() { 11 root = new TrieNode(); 12 } 13 // Adds a word into t

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

Design a data structure that supports the following two operations: addWord(word) and search(word) search(word) can search a literal word or a regular expression string containing only letters a-z or .. A . means it can represent any one letter.Notic

Leetcode: Add and Search Word - Data structure design

Design a data structure that supports the following two operations: void addWord(word) bool search(word) search(word) can search a literal word or a regular expression string containing only letters a-z or .. A . means it can represent any one letter