Program to create a lexically minimal string from two strings in python

Creating a lexically minimal string from two strings involves comparing suffixes and selecting characters that produce the smallest lexicographical result. We compare remaining portions of both strings and choose the character from the string with the lexicographically smaller suffix.

So, if the input is like input_1 = 'TUTORIALS', input_2 = 'POINT', then the output will be POINTTUTORIALS.

Algorithm Steps

If we compare the two strings step-by-step ?

TUTORIALS vs POINT
TUTORIALS vs OINT ? P (choose from input_2)
TUTORIALS vs INT ? O (choose from input_2) 
TUTORIALS vs NT ? I (choose from input_2)
TUTORIALS vs T ? N (choose from input_2)
TUTORIALS vs "" ? T (choose from input_1, input_2 is empty)

The algorithm works as follows ?

  • Add sentinel character 'z' to both strings to handle empty string comparisons
  • Use two pointers to track current position in each string
  • Compare remaining suffixes of both strings lexicographically
  • Choose character from string with smaller suffix
  • Remove sentinel character and append any remaining characters

Implementation

def solve(input_1, input_2):
    input_1 += "z"
    input_2 += "z"
    temp_1 = 0
    temp_2 = 0
    res_str = ""
    
    while temp_1 < len(input_1) and temp_2 < len(input_2):
        if input_1[temp_1:] < input_2[temp_2:]:
            res_str += input_1[temp_1]
            temp_1 += 1
        else:
            res_str += input_2[temp_2]
            temp_2 += 1
    
    # Remove sentinel character
    res_str = res_str[:-1]
    
    # Append remaining characters (excluding sentinel)
    if temp_1 < len(input_1):
        res_str += input_1[temp_1:-1]
    if temp_2 < len(input_2):
        res_str += input_2[temp_2:-1]
    
    return res_str

print(solve('TUTORIALS', 'POINT'))
POINTTUTORIALS

How It Works

The key insight is suffix comparison. At each step, we compare the remaining portions of both strings:

def demonstrate_comparisons():
    s1, s2 = "TUTORIALS", "POINT"
    
    print("Step-by-step suffix comparisons:")
    i, j = 0, 0
    
    while i < len(s1) and j < len(s2):
        suffix1 = s1[i:]
        suffix2 = s2[j:]
        
        print(f"'{suffix1}' vs '{suffix2}' ? ", end="")
        
        if suffix1 < suffix2:
            print(f"Choose '{s1[i]}' from first string")
            i += 1
        else:
            print(f"Choose '{s2[j]}' from second string")
            j += 1
    
    # Handle remaining characters
    if i < len(s1):
        print(f"Remaining from first string: '{s1[i:]}'")
    if j < len(s2):
        print(f"Remaining from second string: '{s2[j:]}'")

demonstrate_comparisons()
Step-by-step suffix comparisons:
'TUTORIALS' vs 'POINT' ? Choose 'P' from second string
'TUTORIALS' vs 'OINT' ? Choose 'O' from second string
'TUTORIALS' vs 'INT' ? Choose 'I' from second string
'TUTORIALS' vs 'NT' ? Choose 'N' from second string
'TUTORIALS' vs 'T' ? Choose 'T' from second string
Remaining from first string: 'TUTORIALS'

Alternative Example

# Test with different strings
print(solve('ABC', 'DEF'))
print(solve('ZEBRA', 'APPLE'))
print(solve('HELLO', 'WORLD'))
ABCDEF
APPLEZEBRA
HELLOWORLD

Conclusion

This algorithm creates the lexically smallest string by comparing suffixes at each step and choosing characters from the string with the smaller remaining suffix. The sentinel character ensures proper handling of empty strings during comparison.

Updated on: 2026-03-26T15:06:58+05:30

216 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements