Count strings from given array having all characters appearing in a given string | Function to count the number of strings from an array having all characters appearing in the string S ; Initialize a set to store all distinct characters of S ; Traverse over S ; Insert characters into the Set ; Stores the required count ; Traverse the array ; Traverse over arr [ i ] ; Check if character in arr [ i ] [ j ] is present in the S or not ; Increment the count if all the characters of arr [ i ] are present in the S ; Finally , prthe count ; Driver code
def countStrings ( S , list ) : NEW_LINE INDENT valid = { } NEW_LINE for x in S : NEW_LINE INDENT valid [ x ] = 1 NEW_LINE DEDENT cnt = 0 NEW_LINE for i in range ( len ( list ) ) : NEW_LINE INDENT j = 0 NEW_LINE while j < len ( list [ i ] ) : NEW_LINE INDENT if ( list [ i ] [ j ] in valid ) : NEW_LINE INDENT j += 1 NEW_LINE continue NEW_LINE DEDENT else : NEW_LINE INDENT break NEW_LINE DEDENT j += 1 NEW_LINE DEDENT if ( j == len ( list [ i ] ) ) : NEW_LINE INDENT cnt += 1 NEW_LINE DEDENT DEDENT return cnt NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT arr = [ " ab " , " aab " , " abaaaa " , " bbd " ] NEW_LINE S , l = " ab " , [ ] NEW_LINE print ( countStrings ( S , arr ) ) NEW_LINE DEDENT