Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Find a string in lexicographic order which is in between given two strings in Python
Finding a string that is lexicographically between two given strings is a common problem in string manipulation. Given two strings S and T of the same length, we need to find a string that is lexicographically greater than S and smaller than T.
A string S = S1S2...Sn is lexicographically smaller than T = T1T2...Tn if there exists an index i where S1 = T1, S2 = T2, ..., Si-1 = Ti-1, but Si < Ti.
Algorithm
The approach is to increment the given string S to find the next lexicographically larger string ?
- Start from the rightmost character and move left
- If current character is not 'z', increment it by 1 and return
- If current character is 'z', set it to 'a' and continue to the next character
- Check if the result is lexicographically smaller than T
Example
Let's implement this solution step by step ?
def find_next_string(string):
"""Find the next lexicographically larger string"""
string_list = list(string)
n = len(string_list)
for i in range(n - 1, -1, -1):
if string_list[i] != 'z':
# Increment current character
k = ord(string_list[i])
string_list[i] = chr(k + 1)
return ''.join(string_list)
# Set 'z' to 'a' and continue
string_list[i] = 'a'
return None # No next string possible
def find_between_strings(S, T):
"""Find a string between S and T lexicographically"""
next_string = find_next_string(S)
if next_string and next_string < T:
return next_string
else:
return -1
# Test the function
S = "bbb"
T = "ddd"
result = find_between_strings(S, T)
print(f"String between '{S}' and '{T}': {result}")
String between 'bbb' and 'ddd': bbc
How It Works
For the input S = "bbb" and T = "ddd" ?
- Start with "bbb" and convert to list ['b', 'b', 'b']
- Check rightmost character 'b' - it's not 'z', so increment to 'c'
- Result is "bbc" which is lexicographically between "bbb" and "ddd"
Edge Cases
Let's test with different scenarios ?
def find_between_strings(S, T):
"""Find a string between S and T lexicographically"""
def find_next_string(string):
string_list = list(string)
n = len(string_list)
for i in range(n - 1, -1, -1):
if string_list[i] != 'z':
k = ord(string_list[i])
string_list[i] = chr(k + 1)
return ''.join(string_list)
string_list[i] = 'a'
return None
next_string = find_next_string(S)
if next_string and next_string < T:
return next_string
else:
return -1
# Test cases
test_cases = [
("bbb", "ddd"),
("abc", "abd"),
("zzz", "aaa"),
("cat", "dog")
]
for s, t in test_cases:
result = find_between_strings(s, t)
print(f"Between '{s}' and '{t}': {result}")
Between 'bbb' and 'ddd': bbc Between 'abc' and 'abd': abc Between 'zzz' and 'aaa': -1 Between 'cat' and 'dog': cau
Conclusion
This algorithm efficiently finds a lexicographically intermediate string by incrementing characters from right to left. The time complexity is O(n) where n is the string length, making it optimal for this problem.
