# Python | Lexicographically smallest string in mixed list

Sometimes, we can also encounter a problem in which we are given a mixed list and require to find the lexicographically smallest string that occur in list. This problem can find it’s application day-day programming. Let’s discuss certain ways in which this problem can be solved.

Method #1 : Using min() + isinstance() + list comprehension This task can be performed using the combination of above functions. In this, the min() functions performs the task of finding smallest string and isinstance() is used to check for string.

## Python3

 `# Python3 code to demonstrate working of` `# Lexicographically smallest string in mixed list` `# Using min() + isinstance() + list comprehension`   `# initializing list` `test_list ``=` `[``1``, ``2``, ``4``, "GFG", ``5``, "IS", ``7``, "BEST"]`   `# printing original list` `print``("The original ``list` `is` `: " ``+` `str``(test_list))`   `# Lexicographically smallest string in mixed list` `# Using min() + isinstance() + list comprehension` `res ``=` `min``(sub ``for` `sub ``in` `test_list ``if` `isinstance``(sub, ``str``))`   `# printing result ` `print``("The Lexicographically smallest string ``is` `: " ``+` `str``(res))`

Output :

```The original list is : [1, 2, 4, 'GFG', 5, 'IS', 7, 'BEST']
The Lexicographically smallest string is : BEST```

Time Complexity: O(n*n) where n is the number of elements in the list “test_list”.
Auxiliary Space: O(1), constant extra space is required

Method #2 : Using min() + lambda + filter() The combination of above functions can also be used to perform this particular task. In this we perform the task of list comprehension using filter() and lambda and min function is used to performed the usual task of finding smallest string.

## Python3

 `# Python3 code to demonstrate working of` `# Lexicographically smallest string in mixed list` `# Using min() + lambda + filter()`   `# initializing list` `test_list ``=` `[``1``, ``2``, ``4``, "GFG", ``5``, "IS", ``7``, "BEST"]`   `# printing original list` `print``("The original ``list` `is` `: " ``+` `str``(test_list))`   `# Lexicographically smallest string in mixed list` `# Using min() + lambda + filter()` `res ``=` `min``(``filter``(``lambda` `s:``isinstance``(s, ``str``), test_list))`   `# printing result ` `print``("The Lexicographically smallest string ``is` `: " ``+` `str``(res))`

Output :

```The original list is : [1, 2, 4, 'GFG', 5, 'IS', 7, 'BEST']
The Lexicographically smallest string is : BEST```

Time Complexity: O(n*n) where n is the number of elements in the list “res_list”.
Auxiliary Space: O(n), where n is the number of elements in the new res list

Method#3: Using Recursive method.

Algorithm:

1. Define a recursive function that takes a list as input.
2. If the list is empty, return a high-value string (e.g., ‘zzz’).
3. If the first element of the list is a string, recursively call the function on the rest of the list and return the minimum of the first element and the result of the recursive call.
4. If the first element of the list is not a string, recursively call the function on the rest of the list.
5. Call the recursive function on the input list and return the result.

## Python3

 `# Python3 code to demonstrate working of` `# Lexicographically smallest string in mixed list` `# Using Recursive method` `def` `recursive_min_string(test_list):` `    ``if` `not` `test_list:` `        ``return` `'zzz'` `    ``elif` `isinstance``(test_list[``0``], ``str``):` `        ``return` `min``(test_list[``0``], recursive_min_string(test_list[``1``:]))` `    ``else``:` `        ``return` `recursive_min_string(test_list[``1``:])`   `# initializing list` `test_list ``=` `[``1``, ``2``, ``4``, ``"GFG"``, ``5``, ``"IS"``, ``7``, ``"BEST"``]`   `# printing original list` `print``(``"The original list is : "` `+` `str``(test_list))`   `# Lexicographically smallest string in mixed list` `res ``=` `recursive_min_string(test_list)`   `# printing result` `print``(``"The Lexicographically smallest string is : "` `+` `str``(res))`

Output

```The original list is : [1, 2, 4, 'GFG', 5, 'IS', 7, 'BEST']
The Lexicographically smallest string is : BEST```

Time Complexity: O(n), where n is the length of the input list. The recursive function iterates over the entire list once, so the time complexity is linear in the length of the list.

Auxiliary Space: O(n), where n is the length of the input list. The recursive function creates a new stack frame for each element of the list, so the space complexity is linear in the length of the list.

Method#4: Using heapq:

Algorithm:

1. Initialize the input list, test_list.
2. Initialize an empty list, strings, to store all the string elements of the input list.
3. Use list comprehension to get all the string elements from the input list.
4. Use the heapq.nsmallest() method to get the lexicographically smallest string from the strings list.
5. Print the lexicographically smallest string.

## Python3

 `import` `heapq`   `# initializing list` `test_list ``=` `[``1``, ``2``, ``4``, ``"GFG"``, ``5``, ``"IS"``, ``7``, ``"BEST"``]`   `# printing original list` `print``(``"The original list is : "` `+` `str``(test_list))`   `# Lexicographically smallest string in mixed list` `# Using heapq` `strings ``=` `[sub ``for` `sub ``in` `test_list ``if` `isinstance``(sub, ``str``)]` `res ``=` `heapq.nsmallest(``1``, strings)[``0``]`   `# printing result` `print``(``"The Lexicographically smallest string is : "` `+` `str``(res))` `#This code  is contributed by Jyothi pinjala.`

Output

```The original list is : [1, 2, 4, 'GFG', 5, 'IS', 7, 'BEST']
The Lexicographically smallest string is : BEST
```

Time Complexity:

Creating a list of all string elements using list comprehension takes O(n) time, where n is the length of the input list.
The heapq.nsmallest() method takes O(k log n) time, where k is the number of smallest elements to be retrieved and n is the length of the input list.
Here, k is 1, so the time complexity reduces to O(log n).
Thus, the overall time complexity of the code is O(n + log n), which can be simplified to O(n).
Space Complexity:

An empty list, strings, is created to store the string elements. This takes O(1) space.
The heapq.nsmallest() method creates a heap with the string elements, which takes O(k) space.
Here, k is 1, so the space complexity is O(1).
Thus, the overall space complexity of the code is O(1).

Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape, GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out - check it out now!

Previous
Next