Python | Check if given string can be formed by concatenating string elements of list
Last Updated :
07 Apr, 2023
Given a string ‘str’ and a list of string elements, write a Python program to check whether given string can be formed by concatenating the string elements of list or not.
Examples:
Input : str = 'python'
lst = ['co', 'de', 'py', 'ks', 'on']
Output : False
Input : str = 'geeks'
lst = ['for', 'ge', 'abc', 'ks', 'e', 'xyz']
Output : True
Approach #1 : Using itertools.permutations
We can use all the permutations of the given list and then perform join operation on them. If any join result is equal to the given string, return true, otherwise false.
Python3
from itertools import permutations
def checkList( str , lst):
for i in range ( 2 , len (lst) + 1 ):
for perm in permutations(lst, i):
if ''.join(perm) = = str :
return True
return False
str = 'geeks'
lst = [ 'for' , 'ge' , 'abc' , 'ks' , 'e' , 'xyz' ]
print (checkList( str , lst))
|
Approach #2 : Python RegEx
Python3
import re
def checkList( str , lst):
r = re. compile ( "(?:" + "|" .join(lst) + ")*$" )
if r.match( str ) ! = None :
return True
return False
str = 'geeks'
lst = [ 'for' , 'ge' , 'abc' , 'ks' , 'e' ]
print (checkList( str , lst))
|
Approach#3: Using String.replace() and loop: We iterate over the list element by element and remove that element in string. After all Iteration if string become empty then it can be form by using list of string else it cannot.
Python3
def checkList(str1, list1):
for i in list1:
str1 = str1.replace(i, "")
if str1 = = "":
return True
else : return False
str = 'geeks'
lst = [ 'for' , 'ge' , 'abc' , 'e' , 'ks' , 'xyz' ]
print (checkList( str , lst))
|
Output:
True
Approach#4: Using dynamic programming
this approach uses dynamic programming to check if the given string can be formed by concatenating string elements of the list. It initializes a boolean list and checks if any prefix of the string till that index is present in the given list and if the previous index is also True. If it is, it sets the current index to True. It returns the value at the last index of the list.
Algorithm
1. Initialize a boolean list of length n+1 where n is the length of the given string.
2. Set the 0th index to True since an empty string can always be formed.
3. For each index i, starting from 1, check if any prefix of the string till that index is present in the given list and if the previous index is also True.
4. If it is, set the current index to True.
5. If the last index is True, return True else return False.
Python3
def check_concatenation( str , lst):
n = len ( str )
dp = [ False ] * (n + 1 )
dp[ 0 ] = True
for i in range ( 1 , n + 1 ):
for j in range (i):
if dp[j] and str [j:i] in lst:
dp[i] = True
break
return dp[n]
str = 'geeks'
lst = [ 'for' , 'ge' , 'abc' , 'e' , 'ks' , 'xyz' ]
print (check_concatenation( str , lst))
|
Time complexity: O(n^3), where n is the length of the given string.
Auxiliary Space: O(n).
Approach#5: Using the lambda function
In this approach, we will define a lambda function checkList(str, lst) to check if the string ‘str’ can be formed by concatenating the elements of the list ‘lst’.
Algorithm:
1. For each length i of sub-lists from 2 to length of lst:
a. Generate all possible permutations of length i using itertools.permutations.
b. If the concatenated string of any permutation equals the given string ‘str’, return True.
2. Return False if the function has not yet returned True.
3. In the main block, set the string ‘str‘ and the list ‘lst‘.
4. Call the checkList function with ‘str‘ and ‘lst‘ as arguments and print the result.
Python3
from itertools import permutations
checkList = lambda s, lst: any (''.join(perm) = = s for i in range ( 2 , len (lst) + 1 ) for perm in permutations(lst, i))
s = 'geeks'
lst = [ 'for' , 'ge' , 'abc' , 'ks' , 'e' , 'xyz' ]
print (checkList(s, lst))
|
Time Complexity: O(n^3)
Auxiliary Space: O(n)
METHOD 6:Using recursion
APPROACH:
The program checks if a given string can be formed by concatenating string elements of a list. It does this by recursively checking if each substring of the input string is present in the list.
ALGORITHM:
1.Define a function is_possible that takes two arguments, string and lst.
2..If string is empty, return True.
3.Loop through lst and check if string starts with any of the words.
4.If string starts with a word, call is_possible with remaining string and lst.
5.If recursive call returns True, return True.
6.If none of the words match, return False.
Python3
def is_possible(string, lst):
if string = = "":
return True
for word in lst:
if string.startswith(word):
if is_possible(string[ len (word):], lst):
return True
return False
string = 'geeks'
lst = [ 'for' , 'ge' , 'abc' , 'ks' , 'e' , 'xyz' ]
print (is_possible(string, lst))
|
Time Complexity: O(n^m) where n is the length of the string and m is the length of the list.
Auxiliary Space: O(m) where m is the length of the list.
Please Login to comment...