Python program to split string into k distinct partitions

When working with strings, we sometimes need to split them into equal-sized partitions and remove duplicate characters from each partition. This creates k distinct partitions where each partition contains unique characters only.

Problem Statement

Given a string s and a value k (where k is a factor of the string length), we need to:

  • Split the string into n/k substrings of size k

  • Remove duplicate characters from each substring

  • Maintain the original character order within each partition

For example, with s = "MMPQMMMRM" and k = 3, we get partitions ["MMP", "QMM", "MRM"] which become ["MP", "QM", "MR"] after removing duplicates.

Algorithm Steps

The solution follows these steps:

  • Initialize variables: index counter, result list, character map, and current partition string
  • Iterate through each character in the string
  • When reaching a new partition boundary (every k characters), save the current partition and reset
  • For each character, add it to the current partition only if it hasn't been seen before
  • Return the list of distinct partitions

Implementation

def solve(s, k):
    i = 0
    ret = []
    mp, to_print = {}, ""
    
    while i < len(s):
        # Check if we've completed a partition
        if i % k == 0 and i != 0:
            ret.append(to_print)
            mp, to_print = {}, ""
        
        # Add character if not already in current partition
        if s[i] not in mp:
            mp[s[i]] = 0
            to_print += s[i]
        
        i += 1
    
    # Add the last partition
    ret.append(to_print)
    return ret

# Test the function
s = "MMPQMMMRM"
k = 3
result = solve(s, k)
print(f"Input: '{s}', k = {k}")
print(f"Output: {result}")
Input: 'MMPQMMMRM', k = 3
Output: ['MP', 'QM', 'MR']

How It Works

Let's trace through the example step by step:

Original String: M M P Q M M M R M Partition 1 Partition 2 Partition 3 After Removing Duplicates: M P Q M M R Result: ["MP", "QM", "MR"]

Alternative Approach Using List Comprehension

Here's a more concise version using Python's built-in functions:

def solve_compact(s, k):
    partitions = [s[i:i+k] for i in range(0, len(s), k)]
    return [''.join(dict.fromkeys(partition)) for partition in partitions]

# Test the compact version
s = "MMPQMMMRM"
k = 3
result = solve_compact(s, k)
print(f"Compact solution: {result}")
Compact solution: ['MP', 'QM', 'MR']

Time and Space Complexity

Approach Time Complexity Space Complexity
Manual Loop O(n) O(k)
List Comprehension O(n) O(n)

Conclusion

This algorithm efficiently splits a string into k-sized partitions while removing duplicate characters. The manual approach is more memory-efficient, while the list comprehension version is more concise and readable.

Updated on: 2026-03-26T15:37:20+05:30

623 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements