/
Offer064.java
54 lines (48 loc) · 1.31 KB
/
Offer064.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
package com.yore.offer2;
/**
* @author Yore
* @date 2022/3/7 13:33
* @description
*/
public class Offer064 {
class MagicDictionary {
private String[] dictionary;
/**
* Initialize your data structure here.
*/
public MagicDictionary() {
this.dictionary = null;
}
public void buildDict(String[] dictionary) {
this.dictionary = dictionary;
}
public boolean search(String searchWord) {
boolean result = false;
for (int i = 0; i < dictionary.length; i++) {
result = getResult(searchWord, dictionary[i]);
if (result) {
return true;
}
}
return false;
}
public boolean getResult(String word, String dic) {
if (word.length() != dic.length()) {
return false;
}
int unsame = 0;
for (int i = 0; i < word.length(); i++) {
if (word.charAt(i) != dic.charAt(i)) {
unsame++;
}
if (unsame > 1) {
return false;
}
}
if (unsame == 0) {
return false;
}
return true;
}
}
}