Created
February 12, 2012 16:51
-
-
Save ashpool/1809615 to your computer and use it in GitHub Desktop.
Citerus challenge
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
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