Python Program to Join Equi-hierarchy Strings
Given a list of strings with several hierarchies, the task is to write a python program to join those which have the same hierarchies.
Elements in the same dimension before a dimension change are known to be in the same hierarchy.
Input : test_list = [“gfg “, ” best “, [” for “, ” all “], ” all “, [” CS ” , ” geeks “]]
Output : [‘gfg best ‘, [‘ for all ‘], ‘ all ‘, [‘ CS geeks ‘]]
Explanation : Strings at similar hierarchy as joined.
Input : test_list = [“gfg “, ” best “, [” for “, ” all “], [” CS ” , ” geeks “]]
Output : [‘gfg best ‘, [‘ for all ‘], [‘ CS geeks ‘]]
Explanation : Strings at similar hierarchy as joined.
Method 1 : Using type(), loop, recursion and join()
In this, the list element is checked to be string using type(), if found, the joining task is performed using join for that hierarchy. If element is found to be list, the inner list is recurred for similar logic for next level of hierarchy.
Example:
Python3
def hierjoin(test_list):
res = []
temp = []
val = None
for sub in test_list:
if type (sub) = = str :
temp.append(sub)
else :
res.append(''.join(temp))
temp = []
val = hierjoin(sub)
res.append(val)
if temp ! = []:
res.append(''.join(temp))
return res
test_list = [ "gfg " , " best " , [ " for " , " all " ],
" all " , [ " CS " , " geeks " ]]
print ( "The original list is : " + str (test_list))
res = hierjoin(test_list)
print ( "The joined strings : " + str (res))
|
Output:
The original list is : [‘gfg ‘, ‘ best ‘, [‘ for ‘, ‘ all ‘], ‘ all ‘, [‘ CS ‘, ‘ geeks ‘]]
The joined strings : [‘gfg best ‘, [‘ for all ‘], ‘ all ‘, [‘ CS geeks ‘]]
Time Complexity: O(n)
Auxiliary Space: O(n)
Method 2 : Using join(), map(), recursion and groupby()
In this equihierarchy groups are formed using groupby(). Then map() is used to call the recursion function for inner hierarchy of list strings.
Example:
Python3
from itertools import groupby
def hierjoin(test_list):
return [idx for x, y in groupby(test_list, key = str .__instancecheck__)
for idx in ([''.join(y)] if x else map (hierjoin, y))]
test_list = [ "gfg " , " best " , [ " for " , " all " ],
" all " , [ " CS " , " geeks " ]]
print ( "The original list is : " + str (test_list))
res = hierjoin(test_list)
print ( "The joined strings : " + str (res))
|
Output:
The original list is : [‘gfg ‘, ‘ best ‘, [‘ for ‘, ‘ all ‘], ‘ all ‘, [‘ CS ‘, ‘ geeks ‘]]
The joined strings : [‘gfg best ‘, [‘ for all ‘], ‘ all ‘, [‘ CS geeks ‘]]
The Time and Space Complexity of all the methods is :
Time Complexity: O(n2)
Space Complexity: O(n)
Approach 3: Using isinstance() and recursion
Python3
def hierjoin(test_list):
res = []
temp = []
for sub in test_list:
if type (sub) = = str :
temp.append(sub)
else :
if temp:
res.append(''.join(temp))
temp = []
res.append(hierjoin(sub))
if temp:
res.append(''.join(temp))
return res
test_list = [ "gfg " , " best " , [ " for " , " all " ], " all " , [ " CS " , " geeks " ]]
print (hierjoin(test_list))
|
Output
['gfg best ', [' for all '], ' all ', [' CS geeks ']]
Time Complexity: O(n)
Auxiliary Space: O(n)
This approach uses the built-in function isinstance to check if an element is a string or not. The approach is the same as the first method, but using isinstance instead of type for checking the type of an element. The time and space complexity remains the same.
Last Updated :
14 Mar, 2023
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...