Minimum number of substrings the given string can be splitted into that satisfy the given conditions | Python3 implementation of the approach ; Set to store all the strings from the given array ; To store the required count ; Recursive function to find the count of substrings that can be splitted starting from the index start such that all the substrings are present in the map ; All the chosen substrings are present in the map ; Update the minimum count of substrings ; Starting from the substrings of length 1 that start with the given index ; Get the substring ; If the substring is present in the set ; Recursive call for the rest of the string ; Function that inserts all the strings from the given array in a set and calls the recursive function to find the minimum count of substrings str can be splitted into that satisfy the given condition ; Insert all the strings from the given array in a set ; Find the required count ; Driver code
import sys NEW_LINE uSet = set ( ) ; NEW_LINE minCnt = sys . maxsize ; NEW_LINE def findSubStr ( string , cnt , start ) : NEW_LINE INDENT global minCnt ; NEW_LINE if ( start == len ( string ) ) : NEW_LINE INDENT minCnt = min ( cnt , minCnt ) ; NEW_LINE DEDENT for length in range ( 1 , len ( string ) - start + 1 ) : NEW_LINE INDENT subStr = string [ start : start + length ] ; NEW_LINE if subStr in uSet : NEW_LINE INDENT findSubStr ( string , cnt + 1 , start + length ) ; NEW_LINE DEDENT DEDENT DEDENT def findMinSubStr ( arr , n , string ) : NEW_LINE INDENT for i in range ( n ) : NEW_LINE INDENT uSet . add ( arr [ i ] ) ; NEW_LINE DEDENT findSubStr ( string , 0 , 0 ) ; NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT string = "123456" ; NEW_LINE arr = [ "1" , "12345" , "2345" , "56" , "23" , "456" ] ; NEW_LINE n = len ( arr ) ; NEW_LINE findMinSubStr ( arr , n , string ) ; NEW_LINE print ( minCnt ) ; NEW_LINE DEDENT