Check if concatenation of any permutation of given list of arrays generates the given array

Solution for geeksforgeeks problem

def solve(arr, sub_arrs):

    sub_arr_membership = {}
    curr_indx = [-1 for i in range(0, len(sub_arrs))]
    for idx, sub_arr in enumerate(sub_arrs):
        for sub_arr_idx, element in enumerate(sub_arr):
            sub_arr_membership[element] = (idx, sub_arr_idx)

    for element in arr:
        idx, sub_arr_idx = sub_arr_membership[element]
        if curr_indx[idx] > sub_arr_idx:
            print(f'False')
            return
        else:
            curr_indx[idx] = sub_arr_idx

    print(f'True')

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s