- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Python Program To Get Minimum Element For String Construction
When it is required to get the minimum element to construct a string, the ‘set’ operator, the ‘combinations’ method, the ‘issubset’ method and a simple iteration is required.
Example
Below is a demonstration of the same
from itertools import combinations my_list = ["python", "is", "fun", "to", "learn"] print("The list is :") print(my_list) my_target_str = "onis" my_result = -1 my_set_string = set(my_target_str) complete_val = False for value in range(0, len(my_list) + 1): for sub in combinations(my_list, value): temp_set = set(ele for subl in sub for ele in subl) if my_set_string.issubset(temp_set): my_result = value complete_val = True break if complete_val: break print("The result is :") print(my_result)
Output
The list is : ['python', 'is', 'fun', 'to', 'learn'] The result is : 2
Explanation
The required packages are imported into the environment.
A list is defined and is displayed on the console.
Another string is defined.
The string is converted to a list.
The list is iterated over, and different combinations of the elements in the list are obtained.
The list is iterated over and converted to a set.
The ‘issubset’ method is used to check if a certain value belongs to the set.
If yes, a variable is assignd ‘True’ value, and breaks out of the loop.
If this value is ‘True’ in the end, the result is displayed on the console.
- Related Articles
- Program to find minimum element addition needed to get target sum in Python
- Program to find minimum changes required for alternating binary string in Python
- Program to find minimum distance to the target element using Python
- Python – Get Every Element from a String List except for a specified letter
- Program to find minimum deletions to make string balanced in Python
- Program to find minimum number of monotonous string groups in Python
- Program to find minimum insertions to balance a parentheses string using Python
- Program to find minimum cost for painting houses in Python
- Program to find minimum string size that contains given substring in Python
- Program to find minimum number of operations to make string sorted in Python
- Program to determine the minimum cost to build a given string in python
- Java Program to get minimum value with Comparator
- Program to find minimum length of string after deleting similar ends in Python
- Program to check minimum number of characters needed to make string palindrome in Python
- Python – Test for Word construction from character list

Advertisements