Giter Club home page Giter Club logo

acrautocomplete's People

Contributors

acrookston avatar orta avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

Forkers

orta dberre

acrautocomplete's Issues

it is not fast enough for HackerRank challenge

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)
    }
}

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.