Open In App

Python Program to check whether Characters of all string elements are in lexical order or not

Given a list with string elements, the task is to write a Python program to return True if all list elements are sorted. The point of caution here is to keep in mind we are comparing characters of a string list element with other characters of the same list element and not string elements with each other. 

Examples:



Input : test_list = ["dent", "flop", "most", "cent"]
Output : True
Explanation : All characters are sorted.
Input : test_list = ["dent", "flop", "mist", "cent"]
Output : False
Explanation : m > i in mist, hence unordered. So, False is returned.

Method 1: Using loop and sorted()

In this, we iterate for each String and test if all the Strings are ordered using sorted(), if any string is not sorted/ordered, the result is flagged off.



Example:




# Initializing list
test_list = ["dent", "flop", "most", "cent"]
 
# Printing original list
print("The original list is : " + str(test_list))
 
res = True
 
for ele in test_list:
 
    # Checking for ordered string
    if ele != ''.join(sorted(ele)):
        res = False
         
        break
 
# Printing result
print("Are all strings ordered ? : " + str(res))

Output
The original list is : ['dent', 'flop', 'most', 'cent']
Are all strings ordered ? : True

Time Complexity: O(nlogn)
Auxiliary Space: O(1)

Method 2 : Using all() and sorted()

In this, loop is avoided and all the strings to be sorted is checked using all(), which returns True if all the elements return true for certain condition.

Example:




# initializing list
test_list = ["dent", "flop", "most", "cent"]
 
# printing original list
print("The original list is : " + str(test_list))
 
# using all() to check all elements to be sorted
res = all(ele == ''.join(sorted(ele)) for ele in test_list)
 
# printing result
print("Are all strings ordered ? : " + str(res))

Output
The original list is : ['dent', 'flop', 'most', 'cent']
Are all strings ordered ? : True

The Space and Time Complexity for all methods is the same:

Time Complexity: O(n)
Auxiliary Space: O(n)

Method 3: Using map() and lambda

The same can be done using map and all. Here, we use map to apply the sorted function on all elements in the list and all() to check if all elements are sorted.




# initializing list
test_list = ["dent", "flop", "most", "cent"]
  
# printing original list
print("The original list is : " + str(test_list))
  
# using map and all() to check all elements to be sorted
res = all(map(lambda x: x == ''.join(sorted(x)), test_list))
  
# printing result
print("Are all strings ordered ? : " + str(res))

Output
The original list is : ['dent', 'flop', 'most', 'cent']
Are all strings ordered ? : True

Time Complexity: O(n)
Auxiliary Space: O(n)

Method 4: Using any() and generator expression




# initializing list
test_list = ["dent", "flop", "most", "cent"]
 
# printing original list
print("The original list is : " + str(test_list))
 
res = not any(ele != ''.join(sorted(ele)) for ele in test_list)
 
# printing result
print("Are all strings ordered? : " + str(res))

Output
The original list is : ['dent', 'flop', 'most', 'cent']
Are all strings ordered? : True

Time complexity: O(n * log n) since it involves sorting of strings and iterating over the list once with the any() function, where n is the length of the input list.
Auxiliary space: O(1) since it only uses a few temporary variables to store the sorted version of each string, and no additional space is required to create a new list or store the results of the generator expression.

Method 5: Use the reduce() function from the functools module. 

Step-by-step approach:




# import reduce function from functools module
from functools import reduce
 
# initializing list
test_list = ["dent", "flop", "most", "cent"]
 
# printing original list
print("The original list is : " + str(test_list))
 
# using reduce and lambda to check all elements to be sorted
result = reduce(lambda x, y: x and (y == ''.join(sorted(y))), test_list, True)
 
# printing result
print("Are all strings ordered ? : " + str(result))

Output
The original list is : ['dent', 'flop', 'most', 'cent']
Are all strings ordered ? : True

Time Complexity: O(n * log n) due to the use of the sorted() function inside the lambda function. 
Auxiliary Space: O(1) because we are only using a single boolean variable to store the result.


Article Tags :