Search code examples
stringalgorithmcombinatoricsset-cover

Logic to find minimum number of strings for complete coverage of characters


I have set of strings

[abcd, efgh, abefg]

How to find the minimum number of strings that covers all the characters (abcdefgh)

Answer would be abcd and efgh. But what would be the algorithm to find this answer?


Solution

  • Thanks everyone for the response, Finally completed it, have given the algorithm below in simple words as a refernce for others

    Sub optimize_strings()

    Capture list of strings in an array variable & number of strings in an integer

    Initialize array of optimized strings as empty & pointer to it as zero

    Get the list of all characters in an array & number of characters in a variable

    Do While number of characters>0

    Reset the frequency of all characters as zero & then calculate the frequency of all characters in uncovered strings in separate array

    Reset the number of uncovered characters for each strings as zero & then calculate the number of uncovered characters in each strings in separate array

    Sort the characters in characters array in ascending order based on their characters frequency array

    Fetch list of strings that contains the character present in the top of the character array & place them in filtered strings array

    Bubble sort filtered strings array in descending order based on the number of uncovered characters which was stored in step 2 of this loop

    Store the Top of the filtered strings array in optimized strings array & increase its pointer to 1

    Iterate through all the characters in the optimized string & remove all the characters present in it from characters array

    Loop

    Print the result of optimized strings present in optimized strings array

    End Sub