Skip to content

Commit 4959c39

Browse files
authored
feat: add swift implementation to lcof2 problem: No.064 (doocs#3133)
1 parent e566918 commit 4959c39

File tree

2 files changed

+106
-0
lines changed

2 files changed

+106
-0
lines changed

lcof2/剑指 Offer II 064. 神奇的字典/README.md

+56
Original file line numberDiff line numberDiff line change
@@ -257,6 +257,62 @@ func patterns(word string) []string {
257257
*/
258258
```
259259

260+
#### Swift
261+
262+
```swift
263+
class MagicDictionary {
264+
private var words: Set<String>
265+
private var counter: [String: Int]
266+
267+
init() {
268+
words = Set<String>()
269+
counter = [String: Int]()
270+
}
271+
272+
func buildDict(_ dictionary: [String]) {
273+
for word in dictionary {
274+
words.insert(word)
275+
for pattern in patterns(word) {
276+
counter[pattern, default: 0] += 1
277+
}
278+
}
279+
}
280+
281+
func search(_ searchWord: String) -> Bool {
282+
for pattern in patterns(searchWord) {
283+
let count = counter[pattern, default: 0]
284+
if count > 1 || (count == 1 && !words.contains(searchWord)) {
285+
return true
286+
}
287+
}
288+
return false
289+
}
290+
291+
private func patterns(_ word: String) -> [String] {
292+
var result = [String]()
293+
var chars = Array(word)
294+
for i in 0..<chars.count {
295+
let originalChar = chars[i]
296+
chars[i] = "*"
297+
result.append(String(chars))
298+
chars[i] = originalChar
299+
}
300+
return result
301+
}
302+
}
303+
304+
/**
305+
* Example usage:
306+
* let obj = MagicDictionary()
307+
* obj.buildDict(["hello", "hallo", "leetcode"])
308+
* let param_2 = obj.search("hello")
309+
* let param_3 = obj.search("hhllo")
310+
* let param_4 = obj.search("hell")
311+
* let param_5 = obj.search("leetcoded")
312+
*/
313+
314+
```
315+
260316
<!-- tabs:end -->
261317

262318
<!-- solution:end -->
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
class MagicDictionary {
2+
private var words: Set<String>
3+
private var counter: [String: Int]
4+
5+
init() {
6+
words = Set<String>()
7+
counter = [String: Int]()
8+
}
9+
10+
func buildDict(_ dictionary: [String]) {
11+
for word in dictionary {
12+
words.insert(word)
13+
for pattern in patterns(word) {
14+
counter[pattern, default: 0] += 1
15+
}
16+
}
17+
}
18+
19+
func search(_ searchWord: String) -> Bool {
20+
for pattern in patterns(searchWord) {
21+
let count = counter[pattern, default: 0]
22+
if count > 1 || (count == 1 && !words.contains(searchWord)) {
23+
return true
24+
}
25+
}
26+
return false
27+
}
28+
29+
private func patterns(_ word: String) -> [String] {
30+
var result = [String]()
31+
var chars = Array(word)
32+
for i in 0..<chars.count {
33+
let originalChar = chars[i]
34+
chars[i] = "*"
35+
result.append(String(chars))
36+
chars[i] = originalChar
37+
}
38+
return result
39+
}
40+
}
41+
42+
/**
43+
* Example usage:
44+
* let obj = MagicDictionary()
45+
* obj.buildDict(["hello", "hallo", "leetcode"])
46+
* let param_2 = obj.search("hello")
47+
* let param_3 = obj.search("hhllo")
48+
* let param_4 = obj.search("hell")
49+
* let param_5 = obj.search("leetcoded")
50+
*/

0 commit comments

Comments
 (0)