Thursday, November 28, 2024
Google search engine
HomeLanguagesPython Program to check whether Characters of all string elements are in...

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:

Python3




# 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:

Python3




# 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.

Python3




# 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

Python3




# 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 the reduce() function from the functools module.
  • Define a lambda function that takes two strings and compares them by their sorted version.
  • Use the reduce() function to apply the lambda function to each pair of adjacent strings in the test_list.The final result will be a single boolean value indicating whether all the strings in the list are sorted or not

Python3




# 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.

Dominic Rubhabha-Wardslaus
Dominic Rubhabha-Wardslaushttp://wardslaus.com
infosec,malicious & dos attacks generator, boot rom exploit philanthropist , wild hacker , game developer,
RELATED ARTICLES

Most Popular

Recent Comments