Created
December 30, 2015 08:43
-
-
Save chz16/639553c16592a1b29cb1 to your computer and use it in GitHub Desktop.
Trie implementation for a pointless Boggle solver
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
struct LetterNode { | |
var isWord = false | |
var children: [Character: LetterNode] = [:] | |
mutating func insert(var word: String) { | |
if word == "" { | |
isWord = true | |
return | |
} | |
let c = word[word.startIndex] | |
if children[c] == nil { | |
children[c] = LetterNode() | |
} | |
word = String(word.characters.dropFirst()) | |
children[c]!.insert(word) | |
} | |
} | |
let wordsUrl = NSURL(fileURLWithPath: "/usr/share/dict/words") | |
let words = try String(contentsOfURL: wordsUrl, encoding: NSUTF8StringEncoding) | |
var wordTree = LetterNode() | |
for word in words.componentsSeparatedByString("\n") { | |
wordTree.insert(word.lowercaseString) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment