Python Program to check whether Characters of all string elements are in lexical order or not
Last Updated :
15 May, 2023
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))
OutputThe 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))
OutputThe 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))
OutputThe 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))
OutputThe 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))
OutputThe 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.
Similar Reads
Python program to check whether the values of a dictionary are in same order as in a list Given a dictionary, test if values are in order with list values. Input : test_dict = {"gfg" : 4, "is" : 10, "best" : 11}, sub_list = [4, 10, 11] Output : True Explanation : Values are 4, 10, 11, same as list order. Hence True is returned. Input : test_dict = {"gfg" : 4, "is" : 10, "best" : 11}, sub
6 min read
Python Program To Check If A Linked List Of Strings Forms A Palindrome Given a linked list handling string data, check to see whether data is palindrome or not? Examples: Input: a -> bc -> d -> dcb -> a -> NULL Output: True String "abcddcba" is palindrome. Input: a -> bc -> d -> ba -> NULL Output: False String "abcdba" is not palindrome. Reco
2 min read
Python program to check a string for specific characters Here, will check a string for a specific character using different methods using Python. In the below given example a string 's' and char array 'arr', the task is to write a python program to check string s for characters in char array arr. Examples: Input: s = @geeksforgeeks% arr[] = {'o','e','%'}O
4 min read
Python program to Sort a List of Strings by the Number of Unique Characters Given a list of strings. The task is to sort the list of strings by the number of unique characters. Examples: Input : test_list = ['gfg', 'best', 'for', 'geeks'], Output : ['gfg', 'for', 'best', 'geeks'] Explanation : 2, 3, 4, 4 are unique elements in lists. Input : test_list = ['gfg', 'for', 'geek
6 min read
Python program to check if given string is vowel Palindrome Given a string (may contain both vowel and consonant letters), remove all consonants, then check if the resulting string is palindrome or not. Examples: Input : abcuhuvmnba Output : YES Explanation : The consonants in the string "abcuhuvmnba" are removed. Now the string becomes "auua". Input : xayzu
5 min read