Skip to content

Instantly share code, notes, and snippets.

@whatalnk
Created June 26, 2017 09:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save whatalnk/cfc21915228e68d43aa15f8ab51c0838 to your computer and use it in GitHub Desktop.
Save whatalnk/cfc21915228e68d43aa15f8ab51c0838 to your computer and use it in GitHub Desktop.
AtCoder AGC #016
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