Python – Extract Sorted Strings
Last Updated :
25 Apr, 2023
Given a String List, extract all sorted strings.
Input : test_list = ["hint", "geeks", "fins", "Gfg"]
Output : ['hint', 'fins', 'Gfg']
Explanation : Strings in increasing order of characters are extracted.
Input : test_list = ["hint", "geeks", "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
test_list = [ "hint" , "geeks" , "fins" , "Gfg" ]
print ( "The original list is : " + str (test_list))
res = [sub for sub in test_list if ''.join( sorted (sub)) = = sub]
print ( "Sorted Strings : " + str (res))
|
Output
The original list is : ['hint', 'geeks', '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
test_list = [ "hint" , "geeks" , "fins" , "Gfg" ]
print ( "The original list is : " + str (test_list))
res = list ( filter ( lambda sub: ''.join( sorted (sub)) = = sub, test_list))
print ( "Sorted Strings : " + str (res))
|
Output
The original list is : ['hint', 'geeks', 'fins', 'Gfg']
Sorted Strings : ['hint', 'fins', 'Gfg']
Time Complexity: O(n)
Auxiliary Space: O(n)
Method #3 : Using extend()+sort() methods
Python3
test_list = [ "hint" , "geeks" , "fins" , "Gfg" ]
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)
print ( "Sorted Strings : " + str (res))
|
Output
The original list is : ['hint', 'geeks', '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
test_list = [ "hint" , "geeks" , "fins" , "Gfg" ]
print ( "The original list is : " + str (test_list))
res = []
for string in test_list:
if ''.join( sorted (string)) = = string:
res.append(string)
print ( "Sorted Strings : " + str (res))
|
Output
The original list is : ['hint', 'geeks', '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
test_list = [ "hint" , "geeks" , "fins" , "Gfg" ]
print ( "The original list is : " + str (test_list))
res = (string for string in test_list if ''.join( sorted (string)) = = string)
print ( "Sorted Strings : " + str ( list (res)))
|
Output
The original list is : ['hint', 'geeks', '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.
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...