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 ” , ”zambiatek “]]
Output : [‘gfg best ‘, [‘ for all ‘], ‘ all ‘, [‘ CS zambiatek ‘]]
Explanation : Strings at similar hierarchy as joined.
Input : test_list = [“gfg “, ” best “, [” for “, ” all “], [” CS ” , ”zambiatek “]]
Output : [‘gfg best ‘, [‘ for all ‘], [‘ CS zambiatek ‘]]
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 string then appended if type(sub) == str: temp.append(sub) # if list, the string is joined for hierarchy # recurred for inner list else: res.append(''.join(temp)) temp = [] val = hierjoin(sub) res.append(val) if temp != []: res.append(''.join(temp)) return res# initializing listtest_list = ["gfg ", " best ", [" for ", " all "], " all ", [" CS ", "zambiatek "]]# printing original listprint("The original list is : " + str(test_list))# calling recursionres = hierjoin(test_list)# printing resultprint("The joined strings : " + str(res)) |
Output:
The original list is : [‘gfg ‘, ‘ best ‘, [‘ for ‘, ‘ all ‘], ‘ all ‘, [‘ CS ‘, ‘zambiatek ‘]]
The joined strings : [‘gfg best ‘, [‘ for all ‘], ‘ all ‘, [‘ CS zambiatek ‘]]
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 groupbydef hierjoin(test_list): # groups are formed for similar hierarchy using groupby return [idx for x, y in groupby(test_list, key=str.__instancecheck__) for idx in ([''.join(y)] if x else map(hierjoin, y))]# initializing listtest_list = ["gfg ", " best ", [" for ", " all "], " all ", [" CS ", "zambiatek "]]# printing original listprint("The original list is : " + str(test_list))# calling recursionres = hierjoin(test_list)# printing resultprint("The joined strings : " + str(res)) |
Output:
The original list is : [‘gfg ‘, ‘ best ‘, [‘ for ‘, ‘ all ‘], ‘ all ‘, [‘ CS ‘, ‘zambiatek ‘]]
The joined strings : [‘gfg best ‘, [‘ for all ‘], ‘ all ‘, [‘ CS zambiatek ‘]]
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
# Method 3: Using list comprehensionsdef 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# Testtest_list = ["gfg ", " best ", [" for ", " all "], " all ", [" CS ", "zambiatek "]]print(hierjoin(test_list))#This code is contributed by Edula Vinay Kumar Reddy |
['gfg best ', [' for all '], ' all ', [' CS zambiatek ']]
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.



