Home > Backend Development > C++ > Minimally modify a string so that all substrings are different

Minimally modify a string so that all substrings are different

王林
Release: 2023-09-04 14:49:07
forward
690 people have browsed it

Minimally modify a string so that all substrings are different

A string is a specific object that represents the sequence and flow of data characters. A string is a data container, always represented in text format. It is also used for conceptual, comparison, split, concatenate, replace, trim, length, internalize, equality, comparison and substring operations. substring() is a data refining process that extracts data between saved data from start to end. substring() does not change the original string. In a dataset, when we have different characters, they can be represented as different data elements. For example: 'a' and 'r' are different, while 'r' and 'r' are the same. So, a string of say, orange contains 6 different characters. Likewise, the string apple contains only 4 distinct characters.

Suppose, "s" is a string, and we need to find the minimum number of changes required for all substrings to make the string different.

  • The length of the string - 26

  • The given input − T is the test case in the first line, which is an integer. For each test case, there will only be one line containing 26 characters.

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

  • Constraints of logical method flow

    • 1

    • 1

In today's article, we will learn how to modify a string so that all substrings are different.

Algorithm for making substrings different

This is a possible algorithm for operating on a string such that all substrings are distinct while minimizing changes.

  • The first step - start.

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

  • Step 3 - From i = 0 in the outer loop, the string length is reduced by 1.

  • Step 4 - Inner loop starts from j = 0, decrementing the string length by 1.

  • Step 5 − Construct a counting variable using 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 if the frequency of string[j]-'a' is zero.

  • Step 10− If it is zero, increase it by 1.

  • Step 11− Otherwise, break it into an inner loop.

  • Step 12 - If the count is greater than zero, return the count.

  • Step 13 - Otherwise, return -1.

  • Step 14 - Termination.

Syntax for creating all different substrings

string.substring(start, end)
Copy after login

In this syntax, we can see how to make minimal changes to a string such that all substrings are different.

  • parameter

    • Start - A starting position needs to be declared. The index of the first character here is 0.

    • End − It is an optional process located at the end (including but not limited to).

method

Method 1−Find the minimum number of changes that make all substrings of the string different.

Find the minimum number of changes such that all substrings of the string become different

In this method, we will learn how to make all substrings different. Here, every character has to be different. We just need to find the number of characters. If the length of the string is more than 26, then we just need to convert it to a string. Here we will write the same logic in different locales.

Example 1: 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;
}
Copy after login

Output

11
Copy after login
Copy after login
Copy after login

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));
   }
}
Copy after login

Output

11
Copy after login
Copy after login
Copy after login

Example 1: 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))
	
Copy after login

Output

11
Copy after login
Copy after login
Copy after login

in conclusion

Today, in this article, we learned how to make all substrings different with minimal changes. Here we have created some possible codes by following the described algorithm in C, Java and Python. Hopefully this helps you gain a more comprehensive understanding of the subject.

The above is the detailed content of Minimally modify a string so that all substrings are different. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:tutorialspoint.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template