Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add swift implementation to lcof2 problem: No.064 #3133

Merged
merged 1 commit into from
Jun 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
56 changes: 56 additions & 0 deletions lcof2/剑指 Offer II 064. 神奇的字典/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -257,6 +257,62 @@ func patterns(word string) []string {
*/
```

#### Swift

```swift
class MagicDictionary {
private var words: Set<String>
private var counter: [String: Int]

init() {
words = Set<String>()
counter = [String: Int]()
}

func buildDict(_ dictionary: [String]) {
for word in dictionary {
words.insert(word)
for pattern in patterns(word) {
counter[pattern, default: 0] += 1
}
}
}

func search(_ searchWord: String) -> Bool {
for pattern in patterns(searchWord) {
let count = counter[pattern, default: 0]
if count > 1 || (count == 1 && !words.contains(searchWord)) {
return true
}
}
return false
}

private func patterns(_ word: String) -> [String] {
var result = [String]()
var chars = Array(word)
for i in 0..<chars.count {
let originalChar = chars[i]
chars[i] = "*"
result.append(String(chars))
chars[i] = originalChar
}
return result
}
}

/**
* Example usage:
* let obj = MagicDictionary()
* obj.buildDict(["hello", "hallo", "leetcode"])
* let param_2 = obj.search("hello")
* let param_3 = obj.search("hhllo")
* let param_4 = obj.search("hell")
* let param_5 = obj.search("leetcoded")
*/

```

<!-- tabs:end -->

<!-- solution:end -->
Expand Down
50 changes: 50 additions & 0 deletions lcof2/剑指 Offer II 064. 神奇的字典/Solution.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
class MagicDictionary {
private var words: Set<String>
private var counter: [String: Int]

init() {
words = Set<String>()
counter = [String: Int]()
}

func buildDict(_ dictionary: [String]) {
for word in dictionary {
words.insert(word)
for pattern in patterns(word) {
counter[pattern, default: 0] += 1
}
}
}

func search(_ searchWord: String) -> Bool {
for pattern in patterns(searchWord) {
let count = counter[pattern, default: 0]
if count > 1 || (count == 1 && !words.contains(searchWord)) {
return true
}
}
return false
}

private func patterns(_ word: String) -> [String] {
var result = [String]()
var chars = Array(word)
for i in 0..<chars.count {
let originalChar = chars[i]
chars[i] = "*"
result.append(String(chars))
chars[i] = originalChar
}
return result
}
}

/**
* Example usage:
* let obj = MagicDictionary()
* obj.buildDict(["hello", "hallo", "leetcode"])
* let param_2 = obj.search("hello")
* let param_3 = obj.search("hhllo")
* let param_4 = obj.search("hell")
* let param_5 = obj.search("leetcoded")
*/
Loading