Skip to content

Instantly share code, notes, and snippets.

@shurane
Created November 28, 2013 04:59
Show Gist options
  • Save shurane/7687422 to your computer and use it in GitHub Desktop.
Save shurane/7687422 to your computer and use it in GitHub Desktop.
dynamic programming for great win. Problem: putting spaces back into a string of words.
obamasaysheplanstovetothebillinmanhattan
router
doesnt
suck
haha
this
is
more
words
obama
n = length of string
m = number of words in dictionary
l = average length of word
m^(n/l)
m*n*n^2*(n/l)
(n) + (n - 1) + (n - 2) + ... + (0)
# n^2 (startindex,endindex) pairs, memoize all n^2 lookups.
F("") = TRUE
F(string) = any([ string.substring(0,i).isInDictionary() and F(string.substring(i,len(string)))
for i in range len(string)])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment