cswork / OAOpen practice workspace

AmazonOA #74

Get Minimum Operations (Sort with Proper-Substring Sort)

Estimated MediumJudge-ready

Topics (AI-labelled)StringsGreedy

Each operation chooses any non-empty proper substring (not the whole string) and sorts its characters in ascending order. Find the minimum number of operations needed to sort the whole string in ascending order.

Input

A single line containing a lowercase string of length 3..100000.

Output

Output a single integer answer.

Example 1

Input:

zyxpqa

Output:

3

Example 2

Input:

abc

Output:

0

Example 3

Input:

bac

Output:

1

Example notes

Example 1: the first character z is the unique maximum and the last character a is the unique minimum, so 3 operations are needed: sort the first 5, the last 5, and the first 5 characters in turn. Example 2: already sorted, 0 operations. Example 3: the last c is already the maximum, so sorting the first two characters ba is enough, 1 operation.

Editorial

Loading solution…