Minimum distance between Duplicates in a String

Finding the minimum distance between duplicates in a string is a frequently encountered problem in computer science. It involves finding the smallest distance between any two identical characters in a given string. For example, in the string "minimum distance," the minimum distance between the two 'i's is two, as they occur at positions 1 and 3, respectively. This problem can be solved using different programming languages, including C++.

In this tutorial, we will learn a C++ algorithm with implementation to efficiently solve this problem. So let's get into this and learn something new and exciting!

Problem Statement

The goal is to find the smallest gap between two identical characters that repeat in a given non-empty string S of length N. If there are no repeating characters in the string S, the function should return -1.

Let’s understand this problem statement with examples.

Sample Example 1

Input

S = "tutorialspoints"; N = 15


Output

The shortest distance between repeating characters is 1.


Explanation: In the given string "tutorialspoints", the letter 't' appears at index 0, and 2. The minimum distance between these two 't's is 1, which is the output of the program.

Sample Example 2

Input

S = "programming"; N = 11


Output

The shortest distance between repeating characters is 1.

Explanation: In the given string "programming", The minimum distance between repeating characters is "0", since the letter 'm' appears twice consecutively at indices 6 and 7.

Algorithm

The algorithm for the minimum distance between the same repeating characters in a given string:

STEP 1: Define a function 'calculateShortestDistanceBetweenRepeatingChars' that takes a 'std::string' and its length 'len' as input arguments.

STEP 2: Set the minimum distance 'minDistance' to the length of the string, assuming no repeating characters are found.

STEP 3: For each character ‘i’ in the string, iterate over all subsequent characters ‘j’.

STEP 4: If ‘i’ and ‘j’ are the same character and the distance between them is less than the current minDistance, update minDistance to the new distance.

STEP 5: If ‘minDistance’ is still the length of the string, no repeating characters were found, so return -1.

STEP 6: Otherwise, subtract one from ‘minDistance’ to get the shortest distance between repeating characters, and return this value.

STEP 7: In ‘main()’, define a string ‘str’ and its length ‘len’, then call ‘calculateShortestDistanceBetweenRepeatingChars’ and store the result in ‘shortestDistance’.

STEP 8: Output the result: if ‘shortestDistance’ is -1, output "No repeating characters found in the string.", otherwise output "The shortest distance between repeating characters is ‘shortestDistance’.".

Now, after understanding the algorithm it's time to implement the above algorithm using C++. We will do this with the help of an example. So let’s do it!

Example

Implementation of the above algorithm using C++

The below C++ program calculates the shortest distance between two repeating characters in a given string and outputs the result. It does this by iterating over each character in the string and comparing it to all subsequent characters, tracking the minimum distance between repeating characters found so far. The program returns -1 if no repeating characters are found, and otherwise outputs the shortest distance between repeating characters.

#include <iostream>
#include <string>
#include <algorithm>

int calculateShortestDistanceBetweenRepeatingChars(const std::string& str, int len) {
// Set the minimum distance to the length of the string, assuming no repeating characters
int minDistance = str.length();
// For every character present in the given string
for (int i = 0; i < len; i++) {
// For each character that comes after it
for (int j = i + 1; j < len; j++) {
// If the characters are identical and the gap between them is smaller than the current minimum distance
if (str[i] == str[j] && (j - i) < minDistance) {
// Update the minimum distance
minDistance = j - i;
// As this value would be the minimum possible, break from the loop
break;
}
}
}
// If the minimum distance is still the length of the string, no repeating characters were found
if (minDistance == str.length()) {
return -1;
} else {
// Subtract one from the minimum distance to get the shortest distance between repeating characters
return minDistance - 1;
}
}
int main() {
// Example input
std::string str = "tutorialspoint";
int len = str.length();
// Calculate the shortest distance between repeating characters
int shortestDistance = calculateShortestDistanceBetweenRepeatingChars(str, len);
if (shortestDistance == -1) {
std::cout << "No repeating characters found in the string.\n";
} else {
std::cout << "The shortest distance between repeating characters is " << shortestDistance << ".\n";
}
return 0;
}


Output

The shortest distance between repeating characters is 1.


Conclusion

To sum up, we have discussed the problem of finding the minimum distance between the same repeating characters in a given string. We have provided a detailed explanation of the problem statement, along with two input-output examples. We have also presented a C++ program that implements an efficient solution using nested loops to compare each pair of characters. By following the algorithm presented in the program, we can easily find the minimum distance between repeating characters in a given string. This problem is commonly encountered in various programming interviews and coding competitions, and by understanding the solution presented in this tutorial, we can better prepare ourselves to tackle such challenges.

Updated on: 08-Sep-2023

55 Views