Last Updated: 17 Jun, 2022

Distinct Substrings

Easy
Asked in company
Deloitte

Problem statement

You are given a string 'S' of length 'N' consisting of lowercase English letters. You want all substrings of the string 'S' to be distinct. You are allowed to change characters at some positions to some other lowercase English letters.


You have to find the minimum number of changes required to make all the substrings of the string distinct or return -1 if you can't make it.


A substring is a contiguous subsegment of a string. For example, "acab" is a substring of "abacaba" (it starts in position 3 and ends in position 6), but "aa" or "d" aren't substrings of this string.


Hint:
Think about substrings of length one.
For Example:-
Let 'N' = 3, 'S' = "aac".
You can change characters at index 2 to some other character (1-based indexing).
Like 'S' = "abc".
All substrings of 'S' are "a", "b", "c", "ab", "bc", "abc", all of which are distinct.
So the answer is 1.
Input Format:-
First-line contains an integer 'T', which denotes the number of test cases.
For every test case:-
First-line contains an integer 'N', denoting the length of the string 'S'.
Second-line contains the string 'S'.
Output Format:-
For each test case, Return the minimum number of changes required to make all the substrings of the string distinct or return -1 if you can't make it.
Note:-
You don’t need to print anything. Just implement the given function.
Constraints:-
1 <= 'T' <= 10
1 <= 'N' <= 10^5
'S' consists only of lower case english alphabets

The Sum of 'N' overall test cases does not exceed 10^5.
Time Limit: 1 sec

Approaches

01 Approach

Approach:-

 

  • If 'N' is greater than 26, then return -1, as we don't have more than 26 distinct lowercase English alphabets.
  • To make the substrings distinct, we have to make the characters of the string distinct.
  • We can change the repeating characters to any other character.
  • The number of characters we have to change is 'N' - unique characters in string 'S'.


 

 Algorithm:-

 

  • If 'N' is greater than 26, then return -1, as we don't have more than 26 distinct lowercase English alphabets.
  • Create an array of size 26 named ‘occ’.
  • Iterate using 'i' from 1 to 'N' in the string 'S'.
    • Update, occ[S[i] - 'a']++.
  • Initialize a variable 'unique' with 0.
  • Iterate using 'i' from 1 to 26 in array 'occ'.
    • If 'occ[i]' > 0, then increase 'unique' by 1.
  • Return 'N' - 'unique' which are repeating characters we have to change.