Rearrange String K Distance Apart šŸŽÆ

beginner
8 min

Rearrange String K Distance Apart šŸŽÆ

Welcome to another exciting lesson on Data Structures and Algorithms at CodeYourCraft! Today, we're going to dive into a fascinating problem known as "Rearrange String K Distance Apart". This problem is a great way to understand and practice some important concepts in algorithms. Let's get started!

Understanding the Problem šŸ“

Given two strings str1 and str2, and an integer k, the task is to rearrange the characters of str1 so that the frequency of characters in the rearranged string matches with the frequency of characters in str2, with each character being at least k positions away from its original position in the rearranged string.

Here's a simple example to illustrate the problem:

str1 = "abccba" str2 = "adbbac" k = 3

In this case, the rearranged string could be "dcadabac". Let's break down why this is the correct solution:

  1. The characters 'a' and 'b' in str1 are rearranged, and they are at least 3 positions away from their original positions.
  2. The character 'c' in str1 remains unchanged, as it doesn't exist in str2.
  3. The characters 'd' and 'a' in str2 are placed at the beginning and end of the rearranged string, maintaining their frequencies.

Algorithm šŸ’”

The solution to this problem can be approached using the following steps:

  1. Calculate the frequency of each character in str1 and str2.
  2. Sort the characters in str1 based on their frequencies.
  3. Iterate through the sorted characters and place them in the result string, ensuring each character is at least k positions away from its original position.
  4. Fill any remaining gaps in the result string with the missing characters from str2.

Implementation šŸ’»

Here's a Python implementation of the algorithm discussed above:

python
def rearrangeString(str1, str2, k): freq1 = {} freq2 = {} res = [] # Calculate frequency of characters in str1 and str2 for char in str1: if char in freq1: freq1[char] += 1 else: freq1[char] = 1 for char in str2: if char in freq2: freq2[char] += 1 else: freq2[char] = 1 # Sort characters in str1 based on their frequencies sorted_chars = sorted(freq1, key=lambda x: (freq1[x], x)) # Place characters in the result string for char in sorted_chars: count = freq1[char] while count > 0: index = len(res) if index >= k and res[index-k] != char: res.insert(index, char) count -= 1 # Fill any remaining gaps with the missing characters from str2 for char in freq2: while freq2[char] > 0 and char not in res: index = len(res) if index >= k: res.insert(index, char) freq2[char] -= 1 return ''.join(res)

Testing the Solution šŸ’”

Let's test our implementation with the example from earlier:

python
print(rearrangeString("abccba", "adbbac", 3)) # Output: "dcadabac"
Quick Quiz
Question 1 of 1

What is the time complexity of the solution for the Rearrange String K Distance Apart problem?

We hope you enjoyed learning about the Rearrange String K Distance Apart problem! Stay tuned for more interesting topics on Data Structures and Algorithms at CodeYourCraft. Happy coding! šŸ¤–