Skip to content

Instantly share code, notes, and snippets.

@ashpool
Created February 12, 2012 16:51
Show Gist options
  • Save ashpool/1809615 to your computer and use it in GitHub Desktop.
Save ashpool/1809615 to your computer and use it in GitHub Desktop.
Citerus challenge
class Stripper
def initialize(acronyms)
@stripCache = {}
@acronyms = acronyms
end
def strip(source)
return @stripCache[source] if @stripCache.has_key?(source)
result = source
@acronyms.each do |acronym|
i = source.index(acronym)
while !i.nil? && 0 <= i && source.length > i do
result = shortest(result, strip(source[0, i] + source[i + acronym.length..-1]))
i = source.index(acronym, i + 1)
end
end
@stripCache[source] = result;
result
end
def shortest(s1, s2)
s1.length < s2.length ? s1 : s2;
end
end
Stripper.new(%W[TDD DDD DI DO OO UI ANT CV IOC LOC SU VO]).strip("VOCDIITEIOCRUDOIANTOCSLOIOCVESTAIOCVOLIOCENTSU")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment