$used = {} def f(s, r="") if s.size < 2 $used[r + s] = 1 else f(s[1..-1], r + s[0]) f(s[0..-2], r + s[-1]) end end f(gets.chomp) p $used.size