This week question is about finding the maximum number of non-overlapping copies in given string.
Given a string s containing letters and ? wildcards (that can match any letter), and a target pattern string pattern, rearrange the entire string however you like. Return the maximum number of non-overlapping copies of pattern that can appear in the rearranged result.
There have been some past weeks recently that I didn’t even try because they seemed difficult and I knew I’d have to spend too much time on them. This week I was baited by the seeming simplicity of the problem
def maxPatternCopies string, pattern
chars = string.chars
(0..).find do
pattern.each_char.find do |char|
index = chars.index(char) || chars.index("?")
if index then
chars[index] = nil
else
true
end
end
end
end