Java for Leetcode 211 Add and Search word-data structure design

Source: Internet
Author: User

Design a data structure that supports the following and the 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.

For example:

Addword ("bad")
Addword ("Dad")
Addword ("Mad")
Search ("Pad"), false
Search ("Bad")-true
Search (". Ad")-True
Search ("B..")-True
Problem Solving Ideas:

Refer to the previous Java for Leetcode 208 Implement Trie (Prefix Tree) Modification, Java implementation is as follows:

public class Worddictionary extends Trie {public void Addword (String word) {Super.insert (word);} Returns If the word is in the data structure. A word could//contain the dot character '. ' to represent any one letter.public Boolean search (String word) {if (Word = = N ull | | Word.length () = = 0) return False;return search (Word, 0, root);} public boolean search (String word, int depth, Trienode node) {if (depth = = Word.length ()-1) {if (Word.charat (depth)! = ') .‘)  {if (Node.son[word.charat (depth)-' a ']! = null) {node = Node.son[word.charat (Depth)-' a '];return node.isend;} elsereturn false;} for (int i = 0; i <; i++) {if (node.son[i]! = null) {Trienode Ason = node.son[i];if (ason.isend) return true;}} return false;} if (Word.charat (depth)! = '. ')  {if (Node.son[word.charat (depth)-' a ']! = null) {node = Node.son[word.charat (Depth)-' A '];return search (Word, depth + 1, node);} Elsereturn false;} for (int i = 0; i <; i++) {if (node.son[i]! = null) {Trienode Ason = node.son[i];if (Search (Word, depth + 1, Ason)) return true;}} return false;}} Class Trienode {//Initialize your data structure Here.int num;//How many words are passed through this node, that is, the number of occurrences of the node character trienode[] son;//all sons node Boolean I send;//is not the value of the last node Char val;//node Trienode () {this.num = 1;this.son = new Trienode[26];this.isend = false;}} Class Trie {protected Trienode root;public Trie () {root = new Trienode ();} public void Insert (String word) {if (Word = = NULL | | word.length () = = 0) return; Trienode node = this.root;char[] Letters = Word.tochararray (); for (int i = 0; i < word.length (); i++) {Int. POS = Letter S[i]-' a ', if (node.son[pos] = = null) {Node.son[pos] = new Trienode (); node.son[pos].val = Letters[i];} else {Node.son[pos] . num++;} node = Node.son[pos];} Node.isend = true;} Returns If the word is in the Trie.public boolean search (String word) {if (Word = = NULL | | word.length () = = 0) {return false;} Trienode node = root;char[] Letters = Word.tochararray (); for (int i = 0; i < word.length (); i++) {int pos = letters[i] -' a '; if (node.son[pos] = null) {node = Node.son[pos];} else {return false;}} return node.isend;} Returns If there is any word in the trie//this starts with the given Prefix.public boolean startsWith (String prefix) { if (prefix = = NULL | | prefix.length () = = 0) {return false;} Trienode node = root;char[] Letters = Prefix.tochararray (); for (int i = 0; i < prefix.length (); i++) {int pos = letters [i]-' a '; if (node.son[pos]! = null) {node = Node.son[pos];} else {return false;}} return true;}} Your Trie object would be instantiated and called as such://Trie Trie = new Trie ();//Trie.insert ("somestring");//Trie . Search ("key");

Java for Leetcode 211 Add and Search word-data structure design

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.