Minimum Changes to string to make all substrings distinct


A string is a particular object, which represents a sequence and flow of data characters. The strings are the data container which always represented as a text format. It also used to concept, compare, split, join, replace, trim, length, intern, equals, comparison, substring operation. A substring() is a data refining process, which extracts the saved data of between two positions from start to end. A substring () does not change the original string. In a data set when we have different characters, they can be denoted as distinct data elements. For example: 'a' and 'r' are distinct while 'r' and 'r' are same. So, a string say, orange contains 6 distinct characters. Also string, apple contains only 4 distinct characters.

Let’s assume, "s" is s string and we have to find the required minimum number of changes to all substrings to make a distinct string.

  • Length of the string − 26

  • Given Input− T is the test cases on the first line with an integer. For every test case there will be only one line with 26 characters.

  • Output− We will get the minimum number of changes for each test case.

  • Logic method Constraints of the process

    • 1 <= T <= 100

    • 1 <= |s| <= 26

Here in this article today, we will learn how to make changes to a string to make all substrings distinct.

Algorithm to make the substrings distinct

Here is the possible algorithm to a string to make all substrings distinct by making minimum changes.

  • Step 1− Start.

  • Step 2− Use two nested loops to generate substrings.

  • Step 3− Outer loop from i = 0, string length minus 1.

  • Step 4− Inner loop from j = 0, string length minus 1.

  • Step 5− Build the count veriable with zero value.

  • Step 6− Inside the outer loop, create a distinct_character variable.

  • Step 7− Create frequency array.

  • Step 8− Set all elements zero.

  • Step 9− Check the frequency of the string[j] - 'a' is zero or not.

  • Step 10− If, Zero. Then increment it by 1.

  • Step 11− Else, break it into a inner loop.

  • Step 12− If the count is greater than zero then return the count.

  • Step 13− Else, return -1.

  • Step 14− Terminate.

Syntax of create all substrings distinct

string.substring(start, end)

Here in this syntax, we can see how to make minimum changes to a string to make all substrings distinct.

  • Parameters

    • Start − It is required to declare a starting position. Here first character is at index 0.

    • End − It is an optional process at the end position (up to, but not including).

Approach

Approach 1 − Find minimum number of changes to it so that all substrings of the string become distinct.

Find minimum number of changes to it so that all substrings of the string become distinct

Here in this approach, we will learn how to make the all substrings distinct. Here the every character must be different. We just need to find out the numbers of the characters. If the string length is more than 26 then we just need to make it into a string. Here we will write the same logic into different language environments.

Example 1: By Using 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 = "aebaecedabbeedee";
   cout << minChanges(str);
   return 0;
}

Output

11

Example 2: By using Java

import java.lang.*;
import java.util.*;
public class tutorialspoint {
   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 = "aebaecedabbeedee";
      System.out.println(minChanges(str));
   }
}

Output

11

Example 1: By Using Python

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 = "aebaecedabbeedee"
	print(minChanges(str))
	

Output

11

Conclusion

Today, in this article we have learnt how to make all substrings distinct by making minimum changes on it. Here we have created some possible codes by following the mentioned algorithm in C++, Java and Python. Hope this will help you to get a bigger picture of this topic.

Updated on: 05-Apr-2023

418 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements