Created
June 26, 2017 09:42
-
-
Save whatalnk/cfc21915228e68d43aa15f8ab51c0838 to your computer and use it in GitHub Desktop.
AtCoder AGC #016
This file contains 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
s = gets.chomp.split("") | |
n = s.length | |
if s.uniq.length == 1 then | |
puts 0 | |
elsif s.uniq.length == n then | |
puts n / 2 | |
else | |
h = Hash.new(0) | |
s.each do |c| | |
h[c] += 1 | |
end | |
ret = n | |
h.each do |k, v| | |
if v > 1 then | |
ind = [0] | |
cand = [] | |
s.each_with_index do |c, i| | |
if c == k | |
ind << i | |
cand << i - ind[-2] - 1 | |
end | |
end | |
cand[0] += 1 | |
cand << n-1 - ind[-1] | |
ret = [ret, cand.max].min | |
end | |
end | |
puts ret | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment