Minimum number of substrings the given string can be splitted into that satisfy the given conditions | 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
var uSet = new Set ( ) ; var minCnt = 1000000000 ; function findSubStr ( str , cnt , start ) { if ( start == str . length ) { minCnt = Math . min ( cnt , minCnt ) ; } for ( var len = 1 ; len <= ( str . length - start ) ; len ++ ) { var subStr = str . substring ( start , start + len ) ; if ( uSet . has ( subStr ) ) { findSubStr ( str , cnt + 1 , start + len ) ; } } } function findMinSubStr ( arr , n , str ) { for ( var i = 0 ; i < n ; i ++ ) uSet . add ( arr [ i ] ) ; findSubStr ( str , 0 , 0 ) ; } var str = " " ; var arr = [ " " , " " , " " , " " , " " , " " ] ; var n = arr . length ; findMinSubStr ( arr , n , str ) ; document . write ( minCnt ) ;