Data Structures and Algorithms: Reorganize String (No Adjacent Same)

beginner
12 min

Data Structures and Algorithms: Reorganize String (No Adjacent Same)

Welcome to CodeYourCraft, where we turn learners into coders! Today, we're diving into the fascinating world of Data Structures and Algorithms. Specifically, we'll tackle a fun challenge: Reorganize String (No Adjacent Same). Let's get started! šŸŽÆ

Introduction

Reorganize String is an interesting problem that asks us to arrange a given string such that no identical characters appear next to each other. This problem tests our understanding of strings, loops, and arrays, making it perfect for both beginners and intermediates.

Problem Statement

Given a string s, rearrange the characters in it such that no two identical characters appear adjacent to each other. If it's impossible to rearrange the string, return "Impossible".

Solution Approach

We'll approach this problem by following these steps:

  1. Count the frequency of each character in the given string.
  2. Sort the characters based on their frequency.
  3. Starting from the lowest frequency character, place them one by one in the rearranged string.
  4. Whenever we encounter a character with a higher frequency, check if it can be placed without violating the "no adjacent same" rule. If it can, place the character; otherwise, skip it and move to the next higher frequency character.

Example

Let's take the string "aaabbcdd" as an example.

  1. Count the frequency of each character:

    • a: 3
    • b: 2
    • c: 1
    • d: 2
  2. Sort the characters based on their frequency:

    • d (2 times)
    • c (1 time)
    • a (3 times)
    • b (2 times)
  3. Starting from the lowest frequency character, place them one by one:

    • d
    • c
    • a (Since there are already two a's, we skip the third one)
    • b
    • a (Again, we skip the fourth a because it's adjacent to the third one)
    • d

The rearranged string is "dcdabc".

Code Example (Python)

Here's a complete, working Python example:

python
def reorganize_string(s): count = {} for char in s: if char not in count: count[char] = 0 count[char] += 1 sorted_char_freq = sorted(count.items(), key=lambda x: x[1], reverse=True) max_freq = sorted_char_freq[0][1] needed = max_freq * len(s) if sum(count[char] for char in count) != needed: return "Impossible" rearranged = [''] * len(s) for char, freq in sorted_char_freq: insert_index = 0 while insert_index < len(s) and count[rearranged[insert_index]] is not None and count[rearranged[insert_index]] < freq: insert_index += 1 if insert_index < len(s): rearranged[insert_index] = char return ''.join(rearranged)

Quiz Time! šŸ’”

Quick Quiz
Question 1 of 1

Given the string `"aaabbcdd"`, what is the rearranged string after applying the "Reorganize String (No Adjacent Same)" rule?

Now that you've got a feel for the Reorganize String problem, practice more by attempting to solve it for different strings! Happy coding, and keep learning with CodeYourCraft! šŸ“ āœ