Python Program To Get Minimum Elements For String Construction Last Updated : 18 Feb, 2023 Comments Improve Suggest changes Like Article Like Report Given a String, the task is to write a Python program to count the minimum elements required to form a string from list elements. Input : test_list = ["geek", "ring", "sfor", "ok", "woke"], tar_str = "working" Output : 2 Explanation : working can be formed using woke and ring. Input : test_list = ["geek", "ring", "sfor", "ok", "woke"], tar_str = "workinggeek" Output : 3 Explanation : workinggeek can be formed using woke, geek and ring. Method : Using issubset() + set() + combinations() In this, we iterate for a list of strings and form all size combinations, each combination is converted to set and checked to be forming target string using issubset(), if found, the loop is exited and the count is recorded. Python3 # Python3 code to demonstrate working of # Minimum elements for String construction # Using issubset() + set() + combinations() from itertools import combinations # initializing list test_list = ["geek", "ring", "sfor", "ok", "woke"] # printing original list print("The original list is : " + str(test_list)) # initializing target string tar_str = "working" res = -1 set_str = set(tar_str) done = False for val in range(0, len(test_list) + 1): # creating combinations for sub in combinations(test_list, val): # constructing sets of each combinations temp_set = set(ele for subl in sub for ele in subl) # checking if target string has created set as subset if set_str.issubset(temp_set): res = val done = True break if done: break # printing result print("The Minimum count elements for string : " + str(res)) OutputThe original list is : ['geek', 'ring', 'sfor', 'ok', 'woke'] The Minimum count elements for string : 2 Time Complexity: O(n2)Auxiliary Space: O(n) Method : using set intersection. This method involves converting the target string into a set and then iterating over the list of strings. For each string in the list, convert it into a set and find its intersection with the target set. If the size of the intersection is equal to the size of the current string, it means that all characters of the current string are present in the target string and hence the current string can be used to construct the target string. Here's the code for this approach: Python3 def get_min_elements(test_list, tar_str): set_str = set(tar_str) count = 0 for string in test_list: set_string = set(string) if len(set_str & set_string) == len(string): count += 1 set_str -= set_string return count test_list = ["geek", "ring", "sfor", "ok", "woke"] tar_str = "working" print(get_min_elements(test_list, tar_str)) Output2 Time Complexity: O(n * m) where n is the length of the target string and m is the number of strings in the list. Auxiliary Space: O(n + m) Comment More infoAdvertise with us Next Article Python Program To Get Minimum Elements For String Construction manjeet_04 Follow Improve Article Tags : Python Python Programs Python list-programs Python string-programs Practice Tags : python Similar Reads First N letters String Construction - Python The task of constructing a string from the first N letters of a given string in Python involves extracting a specific portion of the string, starting from the beginning and including the first N characters. For example, if we have the string "GeeksForGeeks" and want to extract the first 5 characters 3 min read Python - Test if string contains element from list Testing if string contains an element from list is checking whether any of the individual items in a list appear within a given string.Using any() with a generator expressionany() is the most efficient way to check if any element from the list is present in the list. Pythons = "Python is powerful an 3 min read Python program to Extract Mesh matching Strings Given a character mesh, containing missing characters, match the string which matches the mesh. Example: Input : test_list = ["geeks", "best", "peeks"], mesh = "_ee_s" Output : ['geeks', 'peeks'] Explanation : Elements according to mesh are geeks and peeks. Input : test_list = ["geeks", "best", "tes 4 min read Python program to extract characters in given range from a string list Given a Strings List, extract characters in index range spanning entire Strings list. Input : test_list = ["geeksforgeeks", "is", "best", "for", "geeks"], strt, end = 14, 20 Output : sbest Explanation : Once concatenated, 14 - 20 range is extracted.Input : test_list = ["geeksforgeeks", "is", "best", 4 min read Python program to find String in a List Searching for a string in a list is a common operation in Python. Whether we're dealing with small lists or large datasets, knowing how to efficiently search for strings can save both time and effort. In this article, weâll explore several methods to find a string in a list, starting from the most e 3 min read Python3 Program for Minimum rotations required to get the same string Given a string, we need to find the minimum number of rotations required to get the same string.Examples:Input : s = "geeks"Output : 5Input : s = "aaaa"Output : 1Method 1: The idea is based on below post.A Program to check if strings are rotations of each other or notStep 1 : Initialize result = 0 ( 2 min read Python | Get positional characters from String Sometimes, while working with Python strings, we can have a problem in which we need to create a substring by joining the particular index elements from a string. Let's discuss certain ways in which this task can be performed. Method #1: Using loop This is a brute method in which this task can be pe 5 min read Python3 Program to Find Lexicographically minimum string rotation | Set 1 Write code to find lexicographic minimum in a circular array, e.g. for the array BCABDADAB, the lexicographic minimum is ABBCABDAD.Source: Google Written TestMore Examples: Input: GEEKSQUIZOutput: EEKSQUIZGInput: GFGOutput: FGGInput: GEEKSFORGEEKSOutput: EEKSFORGEEKSGFollowing is a simple solution. 2 min read Concatenate all Elements of a List into a String - Python We are given a list of words and our task is to concatenate all the elements into a single string with spaces in between. For example, given the list: li = ['hello', 'geek', 'have', 'a', 'geeky', 'day'] after concatenation, the result will be: "hello geek have a geeky day".Using str.join()str.join() 2 min read Python - Access element at Kth index in given String Given a String, access element at Kth index. Input : test_str = 'geeksforgeeks', K = 4 Output : s Explanation : s is 4th element Input : test_str = 'geeksforgeeks', K = 24 Output : string index out of range Explanation : Exception as K > string length. Method #1 : Using [] operator This is basic 4 min read Like