Given a String List, extract all sorted strings.
Input : test_list = ["hint", "neveropen", "fins", "Gfg"] Output : ['hint', 'fins', 'Gfg'] Explanation : Strings in increasing order of characters are extracted.
Input : test_list = ["hint", "neveropen", "Gfg"] Output : ['hint', 'Gfg'] Explanation : Strings in increasing order of characters are extracted.
Method #1 : Using list comprehension + sorted()
In this, we perform the task of sorting strings and comparison using sorted(), list comprehension is used to iterate through Strings.
Python3
# Python3 code to demonstrate working of # Extract sorted strings # Using list comprehension + sorted() # initializing list test_list = [ "hint" , "neveropen" , "fins" , "Gfg" ] # printing original list print ( "The original list is : " + str (test_list)) # sorted(), converts to sorted version and compares with # original string res = [sub for sub in test_list if ''.join( sorted (sub)) = = sub] # printing result print ( "Sorted Strings : " + str (res)) |
The original list is : ['hint', 'neveropen', 'fins', 'Gfg'] Sorted Strings : ['hint', 'fins', 'Gfg']
Time complexity: O(nlogn + n * klogk), where n is the number of strings in the list and k is the maximum length of a string in the list.
Auxiliary space: O(n), as we are creating a new list with the sorted strings.
Method #2 : Using filter() + lambda + sorted() + join()
In this, we perform filtering using filter() + lambda, and join() is used to convert the final sorted character list result to a string for comparison.
Python3
# Python3 code to demonstrate working of # Extract sorted strings # Using filter() + lambda + sorted() + join() # initializing list test_list = [ "hint" , "neveropen" , "fins" , "Gfg" ] # printing original list print ( "The original list is : " + str (test_list)) # sorted(), converts to sorted version and compares with # original string res = list ( filter ( lambda sub: ''.join( sorted (sub)) = = sub, test_list)) # printing result print ( "Sorted Strings : " + str (res)) |
The original list is : ['hint', 'neveropen', 'fins', 'Gfg'] Sorted Strings : ['hint', 'fins', 'Gfg']
Time Complexity: O(n)
Auxiliary Space: O(n)
Method #3 : Using extend()+sort() methods
Python3
# Python3 code to demonstrate working of # Extract sorted strings # initializing list test_list = [ "hint" , "neveropen" , "fins" , "Gfg" ] # printing original list print ( "The original list is : " + str (test_list)) res = [] for i in test_list: x = list (i) y = [] y.extend(x) y.sort() if (x = = y): res.append(i) # printing result print ( "Sorted Strings : " + str (res)) |
The original list is : ['hint', 'neveropen', 'fins', 'Gfg'] Sorted Strings : ['hint', 'fins', 'Gfg']
Time Complexity: O(n)
Auxiliary Space: O(n)
Method #4: Using a for loop and a separate list for storing sorted strings
- Initialize an empty list to store the sorted strings.
- Loop through each string in the input list.
- For each string, use the sorted() function to sort the characters in the string in ascending order and then join them back into a string.
- Compare the sorted string obtained in step 3 with the original string.
- If the sorted string is equal to the original string, it means that the original string contains characters in sorted order, so append it to the list of sorted strings.
- After processing all strings in the input list, return the list of sorted strings.
Python3
# Python3 code to demonstrate working of # Extract sorted strings # Using for loop and a separate list for storing sorted strings # initializing list test_list = [ "hint" , "neveropen" , "fins" , "Gfg" ] # printing original list print ( "The original list is : " + str (test_list)) # using for loop to check each string res = [] for string in test_list: if ''.join( sorted (string)) = = string: res.append(string) # printing result print ( "Sorted Strings : " + str (res)) |
The original list is : ['hint', 'neveropen', 'fins', 'Gfg'] Sorted Strings : ['hint', 'fins', 'Gfg']
Time complexity: O(n * klogk), where n is the length of the input list and k is the maximum length of a string in the list, due to the sorting operation.
Auxiliary space: O(n), for storing the output list of sorted strings.
Method #5: Using a generator expression with sorted() and if condition
In this method, we can use a generator expression with sorted() function and an if condition to check if the sorted string is equal to the original string. If it is, we can yield the original string.
Approach:
- Use a generator expression with sorted() function to check if the sorted string is equal to the original string.
- Yield the original string if it is sorted.
Python3
# Python3 code to demonstrate working of # Extract sorted strings # Using generator expression with sorted() and if condition # initializing list test_list = [ "hint" , "neveropen" , "fins" , "Gfg" ] # printing original list print ( "The original list is : " + str (test_list)) # using generator expression to check each string res = (string for string in test_list if ''.join( sorted (string)) = = string) # printing result print ( "Sorted Strings : " + str ( list (res))) |
The original list is : ['hint', 'neveropen', 'fins', 'Gfg'] Sorted Strings : ['hint', 'fins', 'Gfg']
Time complexity: O(n * k log k), where n is the length of the list and k is the length of the longest string in the list.
Auxiliary space: O(n), where n is the length of the list.