Creating the Lexicographically Smallest String with One Swap in Python

Rate this post

πŸ’‘ Problem Formulation: We aim to find a method to create the lexicographically smallest string by making only one swap of characters in a given string. For instance, if the input string is “acb”, one swap of ‘b’ and ‘c’ would yield the desired output “abc”, which is the smallest possible string in lexicographical order that can be achieved with a single swap.

Method 1: Greedy Swap

This method involves identifying the first character that can be swapped with a smaller character occurring later in the string to produce a lexicographically smaller string. We iterate through the string and perform the swap operation as soon as we find such an opportunity. This is an in-place operation and doesn’t require additional space.

Here’s an example:

def lexicographically_smallest_swap(s):
    str_list = list(s)
    for i in range(len(str_list)):
        min_char_index = i + str_list[i:].index(min(str_list[i:]))
        if str_list[i] > str_list[min_char_index]:
            str_list[i], str_list[min_char_index] = str_list[min_char_index], str_list[i]
            return ''.join(str_list)
    return s


Output: “abcca”

The code converts the string into a list to perform a swap. It finds the first character which is not in lexicographic order and swaps it with the smallest character found after it. This greedy approach guarantees the smallest string possible with one swap.

Method 2: Sorting and Swapping

This technique involves sorting a subset of the string to identify a potential character for swapping. We traverse the string and, for each character, we sort the remaining subset of the string to find a candidate for swapping that provides the lexicographically smallest result.

Here’s an example:

def smallest_string_sort_swap(s):
    for i in range(len(s)):
        sorted_subset = sorted(s[i+1:])
        for char in sorted_subset:
            if char < s[i]:
                return s[:i] + char + s[i+1:].replace(char, s[i], 1)
    return s


Output: “abcca”

The function traverses the characters of the string and for each character, sorts the remaining characters and looks for the smallest character that can be swapped to make the string lexicographically smaller. It then replaces the found character with the current character, resulting in the smallest possible string with one swap.

Method 3: Using Heap

This method requires forming a heap of all characters after each index to quickly find the smallest character available for swapping. By using a heap data structure, we can maintain a sorted order of the remaining characters and achieve a more efficient search for the swap candidate.

Here’s an example:

import heapq

def smallest_string_heap(s):
    s = list(s)
    for i in range(len(s)-1):
        heap = s[i+1:]
        min_char = heap[0]
        if s[i] > min_char:
            min_index = s.index(min_char, i+1)
            s[i], s[min_index] = s[min_index], s[i]
            return "".join(s)
    return "".join(s)


Output: “abcca”

By converting the string into a list, the function iteratively creates a heap from the remaining characters and finds the minimum character to swap with. By using a heap, the approach optimizes the search for the lowest lexicographical character, but it incurs the overhead of heap operations.

Method 4: Smart Enumeration

This technique scans the string and keeps track of potential characters that could be swapped. The idea is to quickly enumerate the possible swaps and evaluate them without having to sort or perform heapification, by remembering positions of considered characters.

Here’s an example:

def smallest_string_smart_enum(s):
    s = list(s)
    for i in range(len(s)-1):
        min_char, min_index = s[i], i
        for j in range(i+1, len(s)):
            if s[j] < min_char:
                min_char, min_index = s[j], j
        if min_index != i:
            s[i], s[min_index] = s[min_index], s[i]
            return ''.join(s)
    return ''.join(s)


Output: “abcca”

This function goes through the string and looks for the smallest character occurring after the current character. If any such character is found, a swap is performed and the new string is returned. This approach is more efficient than sorting as it involves only one pass through the remaining characters.

Bonus One-Liner Method 5: Using Min and Replace

Taking advantage of Python’s built-in functions, you can perform the swap in a single line of code by identifying the first character that can be replaced with the smallest occurring later character.

Here’s an example:

swap_min_one_liner = lambda s: s[:s.index(min(filter(lambda c: c > s[s.index(min(s)):].min(), s)))] + min(filter(lambda c: c > s[s.index(min(s)):].min(), s), s[s.index(min(s)):].replace(min(filter(lambda c: c > s[s.index(min(s)):].min(), s)), s[s.index(min(filter(lambda c: c > s[s.index(min(s)):].min(), s)))], 1) if min(s) < max(s) else s


Output: “abcca”

This one-liner function uses Python list comprehension, the min, max, filter, and replace functions to achieve the result in an extremely concise way, although readability is compromised.


  • Method 1: Greedy Swap. It’s simple and straightforward. However, it might not be the most efficient for very long strings due to its linear iteration for finding the minimum in the remaining string.
  • Method 2: Sorting and Swapping. It makes use of sorting to quickly find the swapping candidate. It may be less efficient due to repeated sorting.
  • Method 3: Using Heap. More efficient in terms of finding the minimum element, but heap operations add complexity and overhead.
  • Method 4: Smart Enumeration. Efficient with a single pass enumeration, but it requires careful implementation to track both characters and their indices.
  • Bonus Method 5: Using Min and Replace. Extremely concise but at the cost of readability, potentially impacting maintenance and debugging.