Modify string by inserting characters such that every K-length substring consists of unique characters only


A common task when working with strings is to make sure that a string adheres to certain conditions. One of these conditions could be to ensure that every substring of length K in the string contains unique characters only. This is a frequent requirement in problems related to data encoding, string manipulation, and cryptography.

Problem Statement

The problem we are trying to solve can be stated as follows −

Given a string str and an integer K, modify the string by inserting characters such that every substring of length K in the string contains unique characters only.

Proposed Solution

We can tackle this problem by using a sliding window technique, which is a method for efficiently checking properties of contiguous subarrays or substrings in a larger array or string.

Let's detail the steps of the algorithm −

  • Initialize an empty unordered_map (hashmap) to keep track of the frequency of characters in the current substring.

  • Iterate over the characters in the string using a sliding window of size K.

  • If a character is already in the hashmap, insert new characters until we get a unique character or the size of the sliding window is K.

  • Move the sliding window by one character and repeat the process until we reach the end of the string.

The time complexity of this algorithm is O(n), where n is the length of the string. This is because we are traversing each character in the string once.

Example

Let's look at the C++ code implementing the above algorithm −

#include<bits/stdc++.h>
using namespace std;

string modifyString(string str, int K) {
   int n = str.size();
   string result = "";
   for(int i = 0; i < n; i++) {
      unordered_map<char, int> freq;
      int j = i;
      while(j < n && j < i + K) {
         while(j < n && freq[str[j]]) {
               result += 'a' + (rand() % 26); // insert a random character
         }
         freq[str[j++]]++;
         result += str[j];
      }
      i = j - 1;
   }
   return result;
}

int main() {
   string str = "abcabc";
   int K = 3;
   cout << modifyString(str, K) << endl;
   return 0;
}

Output

bcabc

This code will insert a random lowercase English letter when it encounters a repeating character.

Testcase Example

Let's take an example to understand this better.

Consider the string str = "abcabc" and K = 3.

After running the code, you may get a result like abcxyzabc. The three-character substrings are abc, bcx, cxy, xyz, yza, zab, abc, which all contain unique characters.

Note − The result may vary because we are inserting random characters.

Conclusion

In conclusion, this algorithm provides a way to modify a string to ensure that every K-length substring has unique characters. It's an efficient solution, leveraging the power of the sliding window technique and the flexibility of C++. We encourage you to experiment with different strings and values of K to fully grasp the concept.

Updated on: 18-May-2023

79 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements