Python – Smallest Length String
Last Updated :
05 May, 2023
Sometimes, while working with a lot of data, we can have a problem in which we need to extract the minimum of all the strings in list. This kind of problem can have applications in many domains. Let’s discuss certain ways in which this task can be performed.
Method #1: Using loop
This is the brute method in which we perform this task. In this, we run a loop to keep a memory of the smallest string length and return the string with min length in list.
Python3
test_list = [ 'gfg' , 'is' , 'best' , 'for' , 'geeks' ]
print ( "The original list : " + str (test_list))
min_len = 99999
for ele in test_list:
if len (ele) < min_len:
min_len = len (ele)
res = ele
print ( "Minimum length string is : " + res)
|
Output :
The original list : ['gfg', 'is', 'best', 'for', 'geeks']
Minimum length string is : is
Time Complexity: O(n)
Auxiliary Space: O(1)
Method #2: Using min() + key
This method can also be used to solve this problem. In this, we use inbuilt min() with “len” as key argument to extract the string with the minimum length.
Python3
test_list = [ 'gfg' , 'is' , 'best' , 'for' , 'geeks' ]
print ( "The original list : " + str (test_list))
res = min (test_list, key = len )
print ( "Minimum length string is : " + res)
|
Output :
The original list : ['gfg', 'is', 'best', 'for', 'geeks']
Minimum length string is : is
Time Complexity: O(n)
Auxiliary Space: O(1)
Method #3: Using min() and index() methods
Python3
test_list = [ 'gfg' , 'is' , 'best' , 'for' , 'geeks' ]
print ( "The original list : " + str (test_list))
x = []
for i in test_list:
x.append( len (i))
res = test_list[x.index( min (x))]
print ( "Minimum length string is : " + res)
|
Output
The original list : ['gfg', 'is', 'best', 'for', 'geeks']
Minimum length string is : is
Time Complexity: O(n)
Auxiliary Space: O(n)
Method #4: Using sorted() + key
Python3
test_list = [ 'gfg' , 'is' , 'best' , 'for' , 'geeks' ]
print ( "The original list : " + str (test_list))
res = sorted (test_list, key = len )
print ( "Minimum length string is : " + res[ 0 ])
|
Output
The original list : ['gfg', 'is', 'best', 'for', 'geeks']
Minimum length string is : is
Time Complexity: O(nlogn) where n is the number of elements in the list “test_list”. sorted() + key performs n number of operations.
Auxiliary Space: O(n), extra space is required where n is the number of elements in the list
# Method 5: Using sort() function
In this program, we define a list of strings string_list. This list contains the strings that we want to find the smallest length string from. In this example, we have used the strings “Hello”, “World”, “Python”, and “Programming”.
We then use the sort() method to sort the list in ascending order of string length. The key parameter is set to len, which
Python3
string_list = [ "Hello" , "World" , "Python" , "Programming" ]
string_list.sort(key = len )
print ( "The smallest length string is:" , string_list[ 0 ])
|
Output
The smallest length string is: Hello
Time complexity: O(N logN), where N is the length of the input list. This is because the sort() function used to sort the list has a time complexity of O(N log N) in the average case.
Auxiliary space: O(N), as we are only modifying the original list in place and not creating any new data structures. This means that the space required is proportional to the size of the input list.
Method 6: Using a lambda function and the reduce() method from the functools module.
Steps:
- Import the functools module to use the reduce() method.
- Define a lambda function that takes two arguments and returns the shortest string.
- Use the reduce() method to apply the lambda function to the list and get the shortest string.
- Print the result.
Python3
import functools
test_list = [ 'gfg' , 'is' , 'best' , 'for' , 'geeks' ]
print ( "The original list : " + str (test_list))
res = functools. reduce ( lambda x, y: x if len (x) < len (y) else y, test_list)
print ( "Minimum length string is : " + res)
|
Output
The original list : ['gfg', 'is', 'best', 'for', 'geeks']
Minimum length string is : is
Time complexity: O(n), where n is the number of strings in the list.
Auxiliary space: O()1 required is O(1), as we are not creating any extra data structure.
Share your thoughts in the comments
Please Login to comment...