#K82837. Maximum Height Difference after Modification

    ID: 36064 Type: Default 1000ms 256MiB

Maximum Height Difference after Modification

Maximum Height Difference after Modification

You are given a string that represents the heights of a series of blocks. Each character in the string is a digit (0-9) representing the height of a block. You are allowed to modify the height of each block by exactly 1, either by adding or subtracting one. Your task is to compute the maximum height difference between any two adjacent blocks after the modification. It can be shown that for any valid input with at least two blocks, the maximum difference attainable is 2.

Note: If a test case contains only one block, no adjacent difference exists; in that case, output 0.

The problem can be formalized as follows:

Let \(s\) be a string of length \(n\) (\(n \ge 1\)), where each character \(s_i\) is a digit. For every adjacent pair \((s_i, s_{i+1})\) (for \(1 \le i < n\)), after modifying each digit by \(\pm1\), the maximum possible difference is given by:

\[ \text{result} = \min\left(\max_{1 \le i < n} \big(|s_{i+1} - s_i| + 2\big),\,2\right) \]

For any string with \(n \ge 2\), the answer is always 2.

inputFormat

The first line of the input contains a single integer T (\(1 \le T \le 10^5\)) denoting the number of test cases. The following T lines each contain a string representing the block heights. Each string consists of digits without spaces.

outputFormat

For each test case, print a single integer on a new line — the maximum height difference between any two adjacent blocks after modifying them by exactly 1. If the input string consists of only one digit, print 0.

## sample
5
12345
54321
11111
12121
123456789
2

2 2 2 2

</p>