Python – Character repetition string combinations
Last Updated :
18 Feb, 2023
Given a string list and list of numbers, the task is to write a Python program to generate all possible strings by repeating each character of each string by each number in the list.
Input : test_list = [“gfg”, “is”, “best”], rep_list = [3, 5, 2]
Output : [‘gggfffggg’, ‘iiisss’, ‘bbbeeesssttt’, ‘gggggfffffggggg’, ‘iiiiisssss’, ‘bbbbbeeeeesssssttttt’, ‘ggffgg’, ‘iiss’, ‘bbeesstt’]
Explanation : Each element of ‘gfg’ is repeated 3, 5 and 2 times to output different strings.
Input : test_list = [“gfg”, “is”, “best”], rep_list = [3, 1, 2]
Output : [‘gggfffggg’, ‘iiisss’, ‘bbbeeesssttt’, ‘gfg’, ‘is’, ‘best’, ‘ggffgg’, ‘iiss’, ‘bbeesstt’]
Explanation : Each element of ‘gfg’ is repeated 3, 1 and 2 times to output different strings.
Method #1 : Using join() + loop + list comprehension + * operator
In this, the task of constructing each string is done using join(). The * operator performs the task of creating multiple character occurrences. The nested loop is used to combine each number with each string.
Python3
test_list = [ "gfg" , "is" , "best" ]
print ( "The original list is : " + str (test_list))
rep_list = [ 3 , 5 , 2 ]
res = [''.join(sub * ele1 for sub in ele2)
for ele1 in rep_list for ele2 in test_list]
print ( "All repetition combinations strings : " + str (res))
|
Output
The original list is : ['gfg', 'is', 'best']
All repetition combinations strings : ['gggfffggg', 'iiisss', 'bbbeeesssttt', 'gggggfffffggggg', 'iiiiisssss', 'bbbbbeeeeesssssttttt', 'ggffgg', 'iiss', 'bbeesstt']
Time complexity: O(n^2), where n is the length of the input list.
Auxiliary space: O(nm), where n is the length of the input list and m is the maximum value in the repetition list.
Method #2 : Using product() + join() + loop
The nested loop for generating pairs is avoiding in this method by the use of the product() method. Rest all the functionality remains same as the above method.
Python3
from itertools import product
test_list = [ "gfg" , "is" , "best" ]
print ( "The original list is : " + str (test_list))
rep_list = [ 3 , 5 , 2 ]
res = [''.join(sub * ele1 for sub in ele2)
for ele2, ele1 in product(test_list, rep_list)]
print ( "All repetition combinations strings : " + str (res))
|
Output
The original list is : ['gfg', 'is', 'best']
All repetition combinations strings : ['gggfffggg', 'gggggfffffggggg', 'ggffgg', 'iiisss', 'iiiiisssss', 'iiss', 'bbbeeesssttt', 'bbbbbeeeeesssssttttt', 'bbeesstt']
Time Complexity: O(n2)
Auxiliary Space: O(n)
Share your thoughts in the comments
Please Login to comment...