Thursday, December 26, 2024
Google search engine
HomeLanguagesPython – Smallest Length String

Python – Smallest Length String

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




# Python3 code to demonstrate working of
# Smallest Length String
# using loop
 
# initialize list
test_list = ['gfg', 'is', 'best', 'for', 'Lazyroar']
 
# printing original list
print("The original list : " + str(test_list))
 
# Smallest Length String
# using loop
min_len = 99999
for ele in test_list:
    if len(ele) < min_len:
        min_len = len(ele)
        res = ele
 
# printing result
print("Minimum length string is : " + res)


Output : 

The original list : ['gfg', 'is', 'best', 'for', 'Lazyroar']
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




# Python3 code to demonstrate working of
# Smallest Length String
# using min() + key
 
# initialize list
test_list = ['gfg', 'is', 'best', 'for', 'Lazyroar']
 
# printing original list
print("The original list : " + str(test_list))
 
# Smallest Length String
# using min() + key
res = min(test_list, key=len)
 
# printing result
print("Minimum length string is : " + res)


Output : 

The original list : ['gfg', 'is', 'best', 'for', 'Lazyroar']
Minimum length string is : is

Time Complexity: O(n)
Auxiliary Space: O(1)

Method #3: Using min() and index() methods

Python3




# Python3 code to demonstrate working of
# Smallest Length String
# using loop
 
# initialize list
test_list = ['gfg', 'is', 'best', 'for', 'Lazyroar']
 
# printing original list
print("The original list : " + str(test_list))
 
# Smallest Length String
x = []
for i in test_list:
    x.append(len(i))
res = test_list[x.index(min(x))]
# printing result
print("Minimum length string is : " + res)


Output

The original list : ['gfg', 'is', 'best', 'for', 'Lazyroar']
Minimum length string is : is

Time Complexity: O(n)
Auxiliary Space: O(n)

Method #4: Using sorted() + key

Python3




# Python3 code to demonstrate working of
# Smallest Length String
# using sorted() + key
 
# initialize list
test_list = ['gfg', 'is', 'best', 'for', 'Lazyroar']
 
# printing original list
print("The original list : " + str(test_list))
 
# Smallest Length String
# using sorted() + key
res = sorted(test_list, key=len)
# printing result
print("Minimum length string is : " + res[0])


Output

The original list : ['gfg', 'is', 'best', 'for', 'Lazyroar']
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




# define a list of strings
string_list = ["Hello", "World", "Python", "Programming"]
 
# sort the list in ascending order of string length
string_list.sort(key=len)
 
# print the first element (smallest length string) from the sorted list
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:

  1. Import the functools module to use the reduce() method.
  2. Define a lambda function that takes two arguments and returns the shortest string.
  3. Use the reduce() method to apply the lambda function to the list and get the shortest string.
  4. Print the result.

Python3




import functools
 
# Initialize list
test_list = ['gfg', 'is', 'best', 'for', 'Lazyroar']
 
# Printing original list
print("The original list : " + str(test_list))
 
# Smallest Length String using reduce() and lambda function
res = functools.reduce(lambda x, y: x if len(x) < len(y) else y, test_list)
 
# Printing result
print("Minimum length string is : " + res)


Output

The original list : ['gfg', 'is', 'best', 'for', 'Lazyroar']
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.

RELATED ARTICLES

Most Popular

Recent Comments