Program to partition color list in Python

Suppose we have a list of color strings containing "red", "green" and "blue". We need to partition the list so that red comes before green, and green comes before blue.

So, if the input is like colors = ["blue", "green", "blue", "red", "red"], then the output will be ['red', 'red', 'green', 'blue', 'blue'].

Algorithm

To solve this, we will follow these steps ?

  • Initialize three pointers: red = 0, green = 0, blue = 0
  • For each string in the list:
    • If string is "red": place "red" at red position, "green" at green position, "blue" at blue position, then increment all pointers
    • If string is "green": place "green" at green position, "blue" at blue position, then increment green and blue pointers
    • If string is "blue": place "blue" at blue position, then increment blue pointer
  • Return the partitioned list

Using Three Pointers Approach

This algorithm uses the Dutch National Flag approach to partition colors in-place with O(n) time complexity ?

class Solution:
    def solve(self, colors):
        red = 0
        green = 0
        blue = 0
        
        for color in colors:
            if color == "red":
                colors[blue] = "blue"
                blue += 1
                colors[green] = "green"
                green += 1
                colors[red] = "red"
                red += 1
            elif color == "green":
                colors[blue] = "blue"
                blue += 1
                colors[green] = "green"
                green += 1
            elif color == "blue":
                colors[blue] = "blue"
                blue += 1
        
        return colors

# Test the solution
ob = Solution()
colors = ["blue", "green", "blue", "red", "red"]
result = ob.solve(colors)
print("Original:", ["blue", "green", "blue", "red", "red"])
print("Partitioned:", result)
Original: ['blue', 'green', 'blue', 'red', 'red']
Partitioned: ['red', 'red', 'green', 'blue', 'blue']

Alternative Solution Using Counting

A simpler approach is to count each color and reconstruct the list ?

def partition_colors(colors):
    # Count occurrences of each color
    red_count = colors.count("red")
    green_count = colors.count("green")
    blue_count = colors.count("blue")
    
    # Reconstruct the list
    result = ["red"] * red_count + ["green"] * green_count + ["blue"] * blue_count
    return result

# Test the solution
colors = ["blue", "green", "blue", "red", "red"]
result = partition_colors(colors)
print("Original:", colors)
print("Partitioned:", result)
Original: ['blue', 'green', 'blue', 'red', 'red']
Partitioned: ['red', 'red', 'green', 'blue', 'blue']

How It Works

The three-pointer approach maintains three boundaries:

  • red pointer: marks the end of the red section
  • green pointer: marks the end of the green section
  • blue pointer: marks the end of the blue section

As we iterate through the list, we place each color in its correct position and update the appropriate pointers.

Comparison

Method Time Complexity Space Complexity In-place
Three Pointers O(n) O(1) Yes
Counting O(n) O(n) No

Conclusion

The three-pointer approach efficiently partitions colors in-place using the Dutch National Flag algorithm. The counting method is simpler but requires additional space for the result list.

Updated on: 2026-03-25T11:18:27+05:30

643 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements