Unique
Practice
4.9 (7 votes)
String algorithms
Hashing algorithm
Algorithms
Problem
29% Success 1737 Attempts 20 Points 1s Time Limit 256MB Memory 1024 KB Max Code

Bob is given a string \(S\) of length \(N\) consisting of lowercase English letters. He has given you a task to distribute all the substrings of this string to all the students in the class. It is given that the number of students in a class is the same as the number of substrings of the given string. But there is a problem that any two students must not have the same string.

Help Bob to find the minimum number of characters in the string that needs to be changed so that each student gets the unique string, or print \(-1\) if it is impossible to do so.

Input Format

  • The first line contains an integer \(T\), which denotes the number of test cases.
  • The first line of each test case contains an integer \(N\), denoting the length of the string \(S\).
  • The second line of each test case contains the string \(S\).

Output Format

For each test case, print the minimum number of changes required to make all the substrings of the string distinct or print \(-1\) if you can't make it.

Constraints

\(1 \leq T \leq 10 \\ 1 \leq N \leq 10^5\)

 

Please login to use the editor

You need to be logged in to access the code editor

Loading...

Please wait while we load the editor

Loading...
Results
Custom Input
Run your code to see the output
Submissions
Please login to view your submissions
Similar Problems
Points:20
7 votes
Tags:
Hash functionEasyStringHashing algorithm
Points:20
369 votes
Tags:
Ad-HocString AlgorithmsHash functionEasyVery Easy
Points:20
10 votes
Tags:
StringImplementationRecruitBasic ProgrammingAlgorithmsEasyString AlgorithmsApprovedHashing algorithmHashing algorithm