acrookston / acrautocomplete Goto Github PK
View Code? Open in Web Editor NEWA text auto-complete library written in Swift for iOS using the trie data structure
License: MIT License
A text auto-complete library written in Swift for iOS using the trie data structure
License: MIT License
I am learning about the Trie Data Structure because every day I am trying to solve some challenges on HackerRank website, and I found an interesting problem called: "Tries: Contacts".
This is the link: https://www.hackerrank.com/challenges/ctci-contacts
I am still solving this problem using Swift.
I tried to use your code, and it gives the right answers, but it is not fast enough.
I think it would be interesting for you to make your library faster.
This is the code that I used.
// Enter your code here
// Library
import Foundation
public protocol Searchable : Hashable {
func keywords() -> [String]
}
open class AutoComplete<T : Searchable> {
var nodes : [Character : AutoComplete<T>]?
var items : [T]?
public init() { }
public func insert(_ object: T) {
for string in object.keywords() {
var tokens = tokenize(string)
var at = 0
var max = tokens.count
insert(&tokens, at: &at, max: &max, object: object)
}
}
private func insert(_ tokens: inout [Character], at: inout Int, max: inout Int, object: T) {
if at < max {
let current = tokens[at]
at += 1
if nodes == nil {
nodes = [Character : AutoComplete<T>]()
}
if nodes![current] == nil {
nodes![current] = AutoComplete<T>()
}
nodes![current]!.insert(&tokens, at: &at, max: &max, object: object)
} else {
if items == nil {
items = [T]()
}
items!.append(object)
}
}
public func insert(set: [T]) {
for object in set {
insert(object)
}
}
public func search(_ string: String) -> [T] {
var mergedResults : Set<T>?
for word in string.components(separatedBy: " ") {
var wordResults = Set<T>()
var tokens = tokenize(word)
find(&tokens, into: &wordResults)
if mergedResults == nil {
mergedResults = wordResults
} else {
mergedResults = mergedResults!.intersection(wordResults)
}
}
return mergedResults == nil ? [] : Array(mergedResults!)
}
func insertAll(into results: inout Set<T>) {
if let items = items {
for t in items {
results.insert(t)
}
}
guard let nodes = nodes else {
return
}
for (_, child) in nodes {
child.insertAll(into: &results)
}
}
private func find(_ tokens : inout [Character], into results: inout Set<T>) {
guard tokens.count > 0 else {
insertAll(into: &results)
return
}
guard let nodes = nodes else {
return
}
let current = tokens.remove(at: 0)
nodes[current]?.find(&tokens, into: &results)
}
private func tokenize(_ string: String) -> [Character] {
return Array(string.lowercased().characters)
}
}
class Word : Searchable {
var word : String
init(word: String) {
self.word = word
}
func keywords() -> [String] {
return [word]
}
}
extension Word : Hashable {
var hashValue: Int { return word.hashValue }
static func == (lhs: Word, rhs: Word) -> Bool {
return lhs.word == rhs.word
}
}
// Code
let N = Int(readLine()!)!
let autocomplete = AutoComplete<Word>()
for i in 0..<N {
let line = readLine()!.characters.split(separator: " ").map{ String($0) }
let action = line[0]
let parameter = line[1]
if action == "add" {
autocomplete.insert(Word(word: parameter))
} else if action == "find" {
print(autocomplete.search(parameter).count)
}
}
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.