1
+ package com .github .lifelab .leetcode .problemset ;
2
+
3
+ import java .util .HashMap ;
4
+ import java .util .Map ;
5
+ import java .util .Objects ;
6
+ import java .util .regex .Pattern ;
7
+
8
+ /**
9
+ * 添加与搜索单词 - 数据结构设计 @see https://leetcode-cn.com/problems/add-and-search-word-data-structure-design/
10
+ *
11
+ * @author Weichao Li ([email protected] )
12
+ * @since 2019-06-30
13
+ */
14
+ public class Solution211 {
15
+
16
+ /**
17
+ * hash + cache 实现
18
+ */
19
+ public class WordDictionary1 {
20
+
21
+ private Map <String , String > wd ;
22
+
23
+ private Map <String , Boolean > cache ;
24
+
25
+ /**
26
+ * Initialize your data structure here.
27
+ */
28
+ public WordDictionary1 () {
29
+ this .wd = new HashMap <>();
30
+ this .cache = new HashMap <>();
31
+ }
32
+
33
+ /**
34
+ * Adds a word into the data structure.
35
+ */
36
+ public void addWord (String word ) {
37
+ wd .put (word , word );
38
+ }
39
+
40
+ /**
41
+ * Returns if the word is in the data structure. A word could contain the dot character '.' to represent any one letter.
42
+ */
43
+ public boolean search (String word ) {
44
+ Boolean cacheValue = cache .get (word );
45
+ if (Objects .nonNull (cacheValue ) && cacheValue ) {
46
+ return cacheValue ;
47
+ } else {
48
+ cacheValue = wd .keySet ().parallelStream ().anyMatch (e -> Pattern .matches (word , e ));
49
+ cache .put (word , cacheValue );
50
+ }
51
+ return cacheValue ;
52
+ }
53
+
54
+ }
55
+
56
+ /**
57
+ * tree 实现
58
+ */
59
+ public class WordDictionary2 {
60
+
61
+ class Node {
62
+
63
+ private Node [] next ;
64
+
65
+ private boolean isWord ;
66
+
67
+ public Node () {
68
+ next = new Node [26 ];
69
+ isWord = false ;
70
+ }
71
+ }
72
+
73
+ private Node root ;
74
+
75
+ /**
76
+ * Initialize your data structure here.
77
+ */
78
+ public WordDictionary2 () {
79
+ root = new Node ();
80
+ }
81
+
82
+ /**
83
+ * Adds a word into the data structure.
84
+ */
85
+ public void addWord (String word ) {
86
+ int len = word .length ();
87
+ Node curNode = root ;
88
+ for (int i = 0 ; i < len ; i ++) {
89
+ char curChar = word .charAt (i );
90
+ Node next = curNode .next [curChar - 'a' ];
91
+ if (next == null ) {
92
+ curNode .next [curChar - 'a' ] = new Node ();
93
+ }
94
+ curNode = curNode .next [curChar - 'a' ];
95
+ }
96
+ if (!curNode .isWord ) {
97
+ curNode .isWord = true ;
98
+ }
99
+ }
100
+
101
+ /**
102
+ * Returns if the word is in the data structure. A word could contain the dot character '.' to represent any one letter.
103
+ */
104
+ public boolean search (String word ) {
105
+ return match (word , root , 0 );
106
+ }
107
+
108
+ private boolean match (String word , Node node , int start ) {
109
+ if (start == word .length ()) {
110
+ return node .isWord ;
111
+ }
112
+ char alpha = word .charAt (start );
113
+ if (alpha == '.' ) {
114
+ for (int i = 0 ; i < 26 ; i ++) {
115
+ if (node .next [i ] != null && match (word , node .next [i ], start + 1 )) {
116
+ return true ;
117
+ }
118
+ }
119
+ return false ;
120
+ } else {
121
+ if (node .next [alpha - 'a' ] == null ) {
122
+ return false ;
123
+
124
+ }
125
+ return match (word , node .next [alpha - 'a' ], start + 1 );
126
+ }
127
+ }
128
+ }
129
+
130
+ }
0 commit comments