Different substrings in a string that start and end with given strings | function to return number of different sub - strings ; initially our answer is zero . ; find the length of given strings ; currently make array and initially put zero . ; find occurrence of " a " and " b " in string " s " ; We use a hash to make sure that same substring is not counted twice . ; go through all the positions to find occurrence of " a " first . ; if we found occurrence of " a " . ; then go through all the positions to find occurrence of " b " . ; if we do found " b " at index j then add it to already existed substring . ; if we found occurrence of " b " . ; now add string " b " to already existed substring . ; If current substring is not included already . ; put any non negative integer to make this string as already existed . ; make substring null . ; return answer . ; Driver Code
def numberOfDifferentSubstrings ( s , a , b ) : NEW_LINE INDENT ans = 0 NEW_LINE ls = len ( s ) NEW_LINE la = len ( a ) NEW_LINE lb = len ( b ) NEW_LINE x = [ 0 ] * ls NEW_LINE y = [ 0 ] * ls NEW_LINE for i in range ( ls ) : NEW_LINE INDENT if ( s [ i : la + i ] == a ) : NEW_LINE INDENT x [ i ] = 1 NEW_LINE DEDENT if ( s [ i : lb + i ] == b ) : NEW_LINE INDENT y [ i ] = 1 NEW_LINE DEDENT DEDENT hash = [ ] NEW_LINE curr_substr = " " NEW_LINE for i in range ( ls ) : NEW_LINE INDENT if ( x [ i ] ) : NEW_LINE INDENT for j in range ( i , ls ) : NEW_LINE INDENT if ( not y [ j ] ) : NEW_LINE INDENT curr_substr += s [ j ] NEW_LINE DEDENT if ( y [ j ] ) : NEW_LINE INDENT curr_substr += s [ j : lb + j ] NEW_LINE if curr_substr not in hash : NEW_LINE INDENT ans += 1 NEW_LINE DEDENT hash . append ( curr_substr ) NEW_LINE DEDENT DEDENT curr_substr = " " NEW_LINE DEDENT DEDENT return ans NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT s = " codecppforfood " NEW_LINE begin = " c " NEW_LINE end = " d " NEW_LINE print ( numberOfDifferentSubstrings ( s , begin , end ) ) NEW_LINE DEDENT