# Minimum changes to a string to make all substrings distinct

Given a string, find minimum number of changes to it so that all substrings of the string become distinct.

Examples :

```Input :  str = "aab"
Output : 1
If we change one instance of 'a'
to any character from 'c' to 'z',
we get all distinct substrings.

Input : str = "aa"
Output : 1
```

## Recommended: Please try your approach on {IDE} first, before moving on to the solution.

To make all substrings distinct, every character must be different. So we simply need to count number of repeated characters. If length of string is more than 26, then we cannot convert it into a string with all distinct substrings (Here we assume that string should contain only lower case characters, ‘a’ to ‘z’)

## C++

 `// CPP program to count number of changes ` `// to make all substrings distinct. ` `#include ` `using` `namespace` `std; ` ` `  `const` `int` `MAX_CHAR = 26; ` ` `  `// Returns minimum changes to str so ` `// that no substring is repeated. ` `int` `minChanges(string &str) ` `{ ` `    ``int` `n = str.length(); ` ` `  `    ``// If length is more than maximum ` `    ``// allowed characters, we cannot ` `    ``// get the required string.  ` `    ``if` `(n > MAX_CHAR) ` `       ``return` `-1; ` ` `  `    ``// Variable to store count of ` `    ``// distinct characters ` `    ``int` `dist_count = 0; ` `     `  `    ``// To store counts of different ` `    ``// characters ` `    ``int` `count[MAX_CHAR] = {0}; ` ` `  `    ``for` `(``int` `i = 0; i < n; i++) { ` `        ``if` `(count[str[i] - ``'a'``] == 0) ` `            ``dist_count++; ` `        ``count[(str[i] - ``'a'``)]++; ` `    ``} ` ` `  `    ``// Answer is, n - number of distinct char ` `    ``return` `(n - dist_count); ` `} ` ` `  `// Driver function ` `int` `main() ` `{ ` `    ``string str = ``"aebaecedabbee"``; ` `    ``cout << minChanges(str); ` `    ``return` `0; ` `} `

## Java

 `// JAVA program to count number of changes ` `// to make all substrings distinct. ` `import` `java.lang.*; ` `import` `java.util.*; ` ` `  `class` `GFG ` `{ ` `    ``static` `final` `int` `MAX_CHAR = ``26``; ` `     `  `    ``// Returns minimum changes to str so ` `    ``// that no substring is repeated. ` `    ``public` `static` `int` `minChanges(String str) ` `    ``{ ` `         `  `        ``int` `n = str.length(); ` `         `  `        ``// If length is more than maximum ` `        ``// allowed characters, we cannot ` `        ``// get the required string.  ` `        ``if` `(n > MAX_CHAR) ` `            ``return` `-``1``; ` `         `  `        ``// Variable to store count of ` `        ``// distinct characters  ` `        ``int` `dist_count = ``0``; ` `        ``int` `count[] = ``new` `int``[MAX_CHAR]; ` `         `  `        ``// To store counts of different ` `        ``// characters ` `        ``for``(``int` `i = ``0``; i < MAX_CHAR; i++) ` `            ``count[i] = ``0``; ` `         `  `        ``for` `(``int` `i = ``0``; i < n; i++) ` `        ``{ ` `            ``if``(count[str.charAt(i)-``'a'``] == ``0``) ` `                ``dist_count++; ` `            ``count[str.charAt(i)-``'a'``]++; ` `        ``} ` `         `  `        ``// Answer is, n - number of distinct char ` `        ``return` `(n-dist_count); ` `    ``} ` `     `  `    ``//Driver function ` `    ``public` `static` `void` `main (String[] args) { ` `         `  `        ``String str = ``"aebaecedabbee"``; ` `         `  `        ``System.out.println(minChanges(str)); ` `    ``} ` `} ` ` `  `/* This code is contributed by Akash Singh*/`

## Python3

 `# Python3 program to count number of changes  ` `# to make all substrings distinct.  ` ` `  `MAX_CHAR ``=` `[``26``]  ` ` `  `# Returns minimum changes to str so  ` `# that no substring is repeated.  ` `def` `minChanges(``str``): ` ` `  `    ``n ``=` `len``(``str` `) ` ` `  `    ``# If length is more than maximum  ` `    ``# allowed characters, we cannot  ` `    ``# get the required string.  ` `    ``if` `(n > MAX_CHAR[``0``]):  ` `        ``return` `-``1` ` `  `    ``# Variable to store count of  ` `    ``# distinct characters  ` `    ``dist_count ``=` `0` `     `  `    ``# To store counts of different  ` `    ``# characters  ` `    ``count ``=` `[``0``] ``*` `MAX_CHAR[``0``]  ` ` `  `    ``for` `i ``in` `range``(n):  ` `        ``if` `(count[``ord``(``str``[i]) ``-` `ord``(``'a'``)] ``=``=` `0``) : ` `            ``dist_count ``+``=` `1` `        ``count[(``ord``(``str``[i]) ``-` `ord``(``'a'``))] ``+``=` `1` `     `  `    ``# Answer is, n - number of distinct char  ` `    ``return` `(n ``-` `dist_count)  ` ` `  `# Driver Code  ` `if` `__name__ ``=``=` `'__main__'``: ` `    ``str` `=` `"aebaecedabbee"` `    ``print``(minChanges(``str``)) ` `         `  `# This code is contributed  ` `# by SHUBHAMSINGH10 `

## C#

 `// C# program to count number of changes ` `// to make all substrings distinct. ` `using` `System; ` ` `  `class` `GFG ` `{ ` `    ``static` `int` `MAX_CHAR = 26; ` `     `  `    ``// Returns minimum changes to str so ` `    ``// that no substring is repeated. ` `    ``public` `static` `int` `minChanges(``string` `str) ` `    ``{ ` `         `  `        ``int` `n = str.Length; ` `         `  `        ``// If length is more than maximum ` `        ``// allowed characters, we cannot ` `        ``// get the required string.  ` `        ``if` `(n > MAX_CHAR) ` `            ``return` `-1; ` `         `  `        ``// Variable to store count of ` `        ``// distinct characters  ` `        ``int` `dist_count = 0; ` `        ``int` `[]count = ``new` `int``[MAX_CHAR]; ` `         `  `        ``// To store counts of different ` `        ``// characters ` `        ``for``(``int` `i = 0; i < MAX_CHAR; i++) ` `            ``count[i] = 0; ` `         `  `        ``for` `(``int` `i = 0; i < n; i++) ` `        ``{ ` `            ``if``(count[str[i] - ``'a'``] == 0) ` `                ``dist_count++; ` `            ``count[str[i] - ``'a'``]++; ` `        ``} ` `         `  `        ``// Answer is, n - number of distinct char ` `        ``return` `(n - dist_count); ` `    ``} ` `     `  `    ``//Driver function ` `    ``public` `static` `void` `Main () { ` `         `  `        ``string` `str = ``"aebaecedabbee"``; ` `         `  `        ``Console.WriteLine(minChanges(str)); ` `    ``} ` `} ` ` `  `/* This code is contributed by Akash Singh*/`

## PHP

 ` 26) ` `    ``return` `-1; ` ` `  `    ``// Variable to store count of ` `    ``// distinct characters ` `    ``\$dist_count` `= 0; ` `     `  `    ``// To store counts of different ` `    ``// characters ` `    ``\$count` `= ``array_fill``(0, 26, 0); ` `    ``for` `(``\$i` `= 0; ``\$i` `< ``\$n``; ``\$i``++) ` `    ``{ ` `        ``if` `(``\$count``[ord(``\$str``[``\$i``]) - 97] == 0) ` `            ``\$dist_count``++; ` `        ``\$count``[ord(``\$str``[``\$i``]) -97]++; ` `    ``} ` ` `  `    ``// Answer is, n - number of  ` `    ``// distinct char ` `    ``return` `(``\$n` `- ``\$dist_count``); ` `} ` ` `  `// Driver Code ` `\$str` `= ``"aebaecedabbee"``; ` `echo` `minChanges(``\$str``); ` ` `  `// This code is contributed by mits  ` `?> `

Output :

```8
```

My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

Article Tags :
Practice Tags :

1

Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.