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