Minimum changes to a string to make all substrings distinct
Last Updated :
28 Sep, 2022
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
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’)
Implementation:
C++
#include <bits/stdc++.h>
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;
}
|
Java
import java.lang.*;
import java.util.*;
class GFG
{
static final int MAX_CHAR = 26 ;
public static int minChanges(String str)
{
int n = str.length();
if (n > MAX_CHAR)
return - 1 ;
int dist_count = 0 ;
int count[] = new int [MAX_CHAR];
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' ]++;
}
return (n-dist_count);
}
public static void main (String[] args) {
String str = "aebaecedabbee" ;
System.out.println(minChanges(str));
}
}
|
Python3
MAX_CHAR = [ 26 ]
def minChanges( str ):
n = len ( str )
if (n > MAX_CHAR[ 0 ]):
return - 1
dist_count = 0
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
return (n - dist_count)
if __name__ = = '__main__' :
str = "aebaecedabbee"
print (minChanges( str ))
|
C#
using System;
class GFG
{
static int MAX_CHAR = 26;
public static int minChanges( string str)
{
int n = str.Length;
if (n > MAX_CHAR)
return -1;
int dist_count = 0;
int []count = new int [MAX_CHAR];
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' ]++;
}
return (n - dist_count);
}
public static void Main () {
string str = "aebaecedabbee" ;
Console.WriteLine(minChanges(str));
}
}
|
PHP
<?php
function minChanges( $str )
{
$n = strlen ( $str );
if ( $n > 26)
return -1;
$dist_count = 0;
$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]++;
}
return ( $n - $dist_count );
}
$str = "aebaecedabbee" ;
echo minChanges( $str );
?>
|
Javascript
<script>
var MAX_CHAR = 26;
function minChanges(str)
{
var n = str.length;
if (n > MAX_CHAR)
return -1;
var dist_count = 0;
var count = Array.from({length: MAX_CHAR}, (_, i) => 0);
for ( var i = 0; i < MAX_CHAR; i++)
count[i] = 0;
for ( var i = 0; i < n; i++)
{
if (count[str.charAt(i).charCodeAt(0)- 'a' .charCodeAt(0)] == 0)
dist_count++;
count[str.charAt(i).charCodeAt(0)- 'a' .charCodeAt(0)]++;
}
return (n-dist_count);
}
var str = "aebaecedabbee" ;
document.write(minChanges(str));
</script>
|
Time complexity: O(n) where n is the length of given string
Auxiliary Space: O(1) it is using constant space for variables
Share your thoughts in the comments
Please Login to comment...