Problem:
Score : 300 points

Problem Statement

Snuke can change a string t of length N into a string t' of length N - 1 under the following rule:

For each i (1 ≤ i ≤ N - 1), the i-th character of t' must be either the i-th or (i + 1)-th character of t.

There is a string s consisting of lowercase English letters.
Snuke's objective is to apply the above operation to s repeatedly so that all the characters in s are the same.
Find the minimum necessary number of operations.

Constraints

1 ≤ |s| ≤ 100

s consists of lowercase English letters.

Input

Input is given from Standard Input in the following format:

s

Output

Print the minimum necessary number of operations to achieve the objective.

Sample Input 1

serval

Sample Output 1

3

One solution is: serval → srvvl → svvv → vvv.

Sample Input 2

jackal

Sample Output 2

2

One solution is: jackal → aacaa → aaaa.

Sample Input 3

zzz

Sample Output 3

0

All the characters in s are the same from the beginning.

Sample Input 4

whbrjpjyhsrywlqjxdbrbaomnw

Sample Output 4

8

In 8 operations, he can change s to rrrrrrrrrrrrrrrrrr.

Solution in Scala (2.11.7) (judge verdict: Accepted)


object Main extends App {
    
    def is_ok(s: Array[Char]): Boolean = {
        if (s.isEmpty) {
            true
        } else {
            val c = s.head
            s.map(_ == c).reduceLeft(_ && _)
        }
    }
    
    def solve_impl(s: Array[Char], c: Char, i: Int): Int = {
        if (is_ok(s)) {
            i
        } else {
            var _s = s.tail
            for (i <- 0 until _s.length) {
                if (s(i) == c || s(i + 1) == c) {
                    _s(i) = c
                }
            }
            solve_impl(_s, c, i + 1)
        }
    }
    
    def solve(s: Array[Char]): Int = {
        val sgb = s.groupBy(identity)
        //val m = sgb.map(_._2.length).reduceLeft(_ max _)
        //val set = sgb.filter(_._2.length == m).map(_._1)
        val set = sgb.map(_._1)  
        
        var l = List(1000)
        for (ch <- set) {
            l = solve_impl(s, ch, 0) :: l
        }
        l.reduceLeft(_ min _)
    }
    
    val sc = new java.util.Scanner(System.in)
    val str = sc.next
    
    val s = str.toArray
    
    println(solve(s))
}