Techno Blender
Digitally Yours.

Find the rank of a given combination from an Array of String

0 45


Improve Article

Save Article

Like Article

Improve Article

Save Article

Given an array of string and a string K, the task is to find the rank of the given string if we generate all the combinations of the given array of strings.

Examples:

Input: string = [‘ab’, ‘pq’, ‘nm’], K = ‘pqnm’
Output: 7
Explanation: All combination generated are :-
”   ——————-> rank 1
‘ab’  —————–> rank 2
‘pq’  —————–> rank 3
‘nm’  —————-> rank 4
‘abpq’  ————–> rank 5
‘abnm’  ————–> rank 6
‘pqnm’  ————–> rank 7
‘abpqnm’ ————> rank 8

‘pqnm’ is at rank is generated at rank 7

Input: string = [‘a’, ‘b’], K = ‘aa’
Output: -1
Explanation: ‘aa’ cannot be generated by any combination of given element.

Approach: To solve the problem follow the below idea:

Generates the powerset of a given list of strings using the combinations function from the itertools module and a nested loop. It then calculates the rank of a target string in that powerset using the index function and returns -1 if the target string is not in the powerset. The code defines a list of strings and a target string, calls the get_rank function, and prints the rank.

Below are the steps for the above approach:

  • Import the combinations function from the itertools module.
  • Define a function to generate the powerset of a given list of strings.
    • Initialize the powerset with an empty string.
    • Loop over all possible lengths of subsets from 1 to the length of the input list.
    • Use the combinations function to generate all possible subsets of the current length.
    • Join the elements of each combination into a single string and append it to the powerset.
    • Return the final powerset.
  • Define a function to get the rank of a given string in the powerset of a list of strings.
    • Generate the powerset of the input string list using the get_powerset function.
    • If the target string is not in the powerset, return -1.
    • Otherwise, return the index of the target string in the sorted powerset, plus 1.
  • Get the rank of the target string in the powerset of the input string list using the get_rank function.
  • Print the rank.

Below is the code for the above approach:

Python

from itertools import combinations

  

  

  

def get_powerset(string_list):

  

    

    

    powerset = ['']

  

    

    

    for i in range(1, len(string_list)+1):

  

        

        

        for combination in combinations(string_list, i):

  

            

            

            powerset.append(''.join(combination))

  

    

    return powerset

  

  

  

def get_rank(string_list, k):

  

    

    

    powerset = get_powerset(string_list)

    

    

    if k not in powerset:

        return -1

    

    

    

    return powerset.index(k) + 1

  

  

string_list = ['ab', 'pq', 'nm']

k = 'pqnm'

  

rank = get_rank(string_list, k)

  

print(rank)

Time complexity: O(n * 2n * log(n))  to generate the power set O(2n), sorting the power set  O(n * 2n * log(n)) and finding the index O(n * 2n). 
Auxiliary space: O(2n) to store all generated combinations.


Improve Article

Save Article

Like Article

Improve Article

Save Article

Given an array of string and a string K, the task is to find the rank of the given string if we generate all the combinations of the given array of strings.

Examples:

Input: string = [‘ab’, ‘pq’, ‘nm’], K = ‘pqnm’
Output: 7
Explanation: All combination generated are :-
”   ——————-> rank 1
‘ab’  —————–> rank 2
‘pq’  —————–> rank 3
‘nm’  —————-> rank 4
‘abpq’  ————–> rank 5
‘abnm’  ————–> rank 6
‘pqnm’  ————–> rank 7
‘abpqnm’ ————> rank 8

‘pqnm’ is at rank is generated at rank 7

Input: string = [‘a’, ‘b’], K = ‘aa’
Output: -1
Explanation: ‘aa’ cannot be generated by any combination of given element.

Approach: To solve the problem follow the below idea:

Generates the powerset of a given list of strings using the combinations function from the itertools module and a nested loop. It then calculates the rank of a target string in that powerset using the index function and returns -1 if the target string is not in the powerset. The code defines a list of strings and a target string, calls the get_rank function, and prints the rank.

Below are the steps for the above approach:

  • Import the combinations function from the itertools module.
  • Define a function to generate the powerset of a given list of strings.
    • Initialize the powerset with an empty string.
    • Loop over all possible lengths of subsets from 1 to the length of the input list.
    • Use the combinations function to generate all possible subsets of the current length.
    • Join the elements of each combination into a single string and append it to the powerset.
    • Return the final powerset.
  • Define a function to get the rank of a given string in the powerset of a list of strings.
    • Generate the powerset of the input string list using the get_powerset function.
    • If the target string is not in the powerset, return -1.
    • Otherwise, return the index of the target string in the sorted powerset, plus 1.
  • Get the rank of the target string in the powerset of the input string list using the get_rank function.
  • Print the rank.

Below is the code for the above approach:

Python

from itertools import combinations

  

  

  

def get_powerset(string_list):

  

    

    

    powerset = ['']

  

    

    

    for i in range(1, len(string_list)+1):

  

        

        

        for combination in combinations(string_list, i):

  

            

            

            powerset.append(''.join(combination))

  

    

    return powerset

  

  

  

def get_rank(string_list, k):

  

    

    

    powerset = get_powerset(string_list)

    

    

    if k not in powerset:

        return -1

    

    

    

    return powerset.index(k) + 1

  

  

string_list = ['ab', 'pq', 'nm']

k = 'pqnm'

  

rank = get_rank(string_list, k)

  

print(rank)

Time complexity: O(n * 2n * log(n))  to generate the power set O(2n), sorting the power set  O(n * 2n * log(n)) and finding the index O(n * 2n). 
Auxiliary space: O(2n) to store all generated combinations.

FOLLOW US ON GOOGLE NEWS

Read original article here

Denial of responsibility! Techno Blender is an automatic aggregator of the all world’s media. In each content, the hyperlink to the primary source is specified. All trademarks belong to their rightful owners, all materials to their authors. If you are the owner of the content and do not want us to publish your materials, please contact us by email – [email protected]. The content will be deleted within 24 hours.
Leave a comment