Skip to content

[pull] main from doocs:main #116

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

Merged
merged 12 commits into from
May 13, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
feat: add swift implementation to lcci problem: No.17.17 (doocs#2804)
  • Loading branch information
klever34 authored May 13, 2024
commit ed2c85c5fec40a73b77c5d18fe1994aad10cbe52
70 changes: 70 additions & 0 deletions lcci/17.17.Multi Search/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -246,6 +246,76 @@ func multiSearch(big string, smalls []string) [][]int {
}
```

```swift
class TrieNode {
var idx: Int
var children: [TrieNode?]

init() {
self.idx = -1
self.children = Array(repeating: nil, count: 26)
}
}

class Trie {
private let root: TrieNode

init() {
self.root = TrieNode()
}

func insert(_ word: String, _ index: Int) {
var node = root
for ch in word {
let i = Int(ch.asciiValue! - Character("a").asciiValue!)
if node.children[i] == nil {
node.children[i] = TrieNode()
}
node = node.children[i]!
}
node.idx = index
}

func search(_ word: String) -> [Int] {
var node = root
var results = [Int]()
for ch in word {
let i = Int(ch.asciiValue! - Character("a").asciiValue!)
if node.children[i] == nil {
break
}
node = node.children[i]!
if node.idx != -1 {
results.append(node.idx)
}
}
return results
}
}

class Solution {
func multiSearch(_ big: String, _ smalls: [String]) -> [[Int]] {
let trie = Trie()
for (index, small) in smalls.enumerated() {
trie.insert(small, index)
}

var results = Array(repeating: [Int](), count: smalls.count)
let bigChars = Array(big)

for i in 0..<bigChars.count {
let substring = String(bigChars[i...])
let indices = trie.search(substring)
for index in indices {
results[index].append(i)
}
}

return results
}
}
```

<!-- tabs:end -->

<!-- end -->
70 changes: 70 additions & 0 deletions lcci/17.17.Multi Search/README_EN.md
Original file line number Diff line number Diff line change
Expand Up @@ -251,6 +251,76 @@ func multiSearch(big string, smalls []string) [][]int {
}
```

```swift
class TrieNode {
var idx: Int
var children: [TrieNode?]

init() {
self.idx = -1
self.children = Array(repeating: nil, count: 26)
}
}

class Trie {
private let root: TrieNode

init() {
self.root = TrieNode()
}

func insert(_ word: String, _ index: Int) {
var node = root
for ch in word {
let i = Int(ch.asciiValue! - Character("a").asciiValue!)
if node.children[i] == nil {
node.children[i] = TrieNode()
}
node = node.children[i]!
}
node.idx = index
}

func search(_ word: String) -> [Int] {
var node = root
var results = [Int]()
for ch in word {
let i = Int(ch.asciiValue! - Character("a").asciiValue!)
if node.children[i] == nil {
break
}
node = node.children[i]!
if node.idx != -1 {
results.append(node.idx)
}
}
return results
}
}

class Solution {
func multiSearch(_ big: String, _ smalls: [String]) -> [[Int]] {
let trie = Trie()
for (index, small) in smalls.enumerated() {
trie.insert(small, index)
}

var results = Array(repeating: [Int](), count: smalls.count)
let bigChars = Array(big)

for i in 0..<bigChars.count {
let substring = String(bigChars[i...])
let indices = trie.search(substring)
for index in indices {
results[index].append(i)
}
}

return results
}
}
```

<!-- tabs:end -->

<!-- end -->
67 changes: 67 additions & 0 deletions lcci/17.17.Multi Search/Solution.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
class TrieNode {
var idx: Int
var children: [TrieNode?]

init() {
self.idx = -1
self.children = Array(repeating: nil, count: 26)
}
}

class Trie {
private let root: TrieNode

init() {
self.root = TrieNode()
}

func insert(_ word: String, _ index: Int) {
var node = root
for ch in word {
let i = Int(ch.asciiValue! - Character("a").asciiValue!)
if node.children[i] == nil {
node.children[i] = TrieNode()
}
node = node.children[i]!
}
node.idx = index
}

func search(_ word: String) -> [Int] {
var node = root
var results = [Int]()
for ch in word {
let i = Int(ch.asciiValue! - Character("a").asciiValue!)
if node.children[i] == nil {
break
}
node = node.children[i]!
if node.idx != -1 {
results.append(node.idx)
}
}
return results
}
}

class Solution {
func multiSearch(_ big: String, _ smalls: [String]) -> [[Int]] {
let trie = Trie()
for (index, small) in smalls.enumerated() {
trie.insert(small, index)
}

var results = Array(repeating: [Int](), count: smalls.count)
let bigChars = Array(big)

for i in 0..<bigChars.count {
let substring = String(bigChars[i...])
let indices = trie.search(substring)
for index in indices {
results[index].append(i)
}
}

return results
}
}