AlgoUniversity
  • Go Back
Discussion
Make String Unique :

Author

hardik kapoor

Difficulty Level : Medium

Submissions : 226

Asked In : Paytm

Marks :10

: 3 | : 0

We are given a string s of length N consisting of only latin lowercase letters.

We can do the following operation atmost once:

Select two integers i , j (1 <= i <= j <= N), remove the string s[i....j] and concatenate the rest. Cost of such an operation is (j-i+1).

For example, in the string "abddefg",if we choose i=3 and j=5, the string gets changes to "abfg".

Find the minimum cost such that after the operation, the string contains only unique characters, or , the string does not contain any character whose occurrence is more than one.

Please note that if the string is already unique, the cost would be 0.

Input

The first line contains T (1 <= T <= 100) - the number of test cases.

Each test case contains s (1 <= |s| <= 1e6) - the string consisting of latin lowercase letters a-z.

Sum of length of string <= 1e6 over all test cases.

Output

For every test case, output one integer, the minimum cost.

Example

Input
3
aaaaa
abbeb
babbababab
Output
4
2
8

Note

For test - 1 ,'a' is the string which remains after the operation

For test - 2, 'aeb' is the string which remains after the operation.

For test - 3, 'ba' is the string which remains after the operation.

You need to login to view your submissions.

You need to login to view all submissions.

Loading...

Result : Executed

Loading...

Feel something is wrong with the test cases?

Result : Accepted

Test Cases :

You need to Log In
We're glad that you want to attempt this problem!

But to Run or Submit the Problem, you need to Log In.

Continue to Log In
Challenge Submitted!

Your challenge has been submitted successfully.

You will get a response soon via WhatsApp or Email.

Challenge
Facing issue while trying to solve the problem! Don't worry, we got you covered!

Do let us know your issue.

Looks good!
Please enter your issue / feedback.

How do we get in touch with you?
Looks good!
Please enter your phone no.
Looks good!
Please enter your email address.