Count of distinct characters in a substring by given range for Q queries | Python3 program for Naive Approach ; Counter to count distinct char ; Initializing frequency array to count characters as the appear in substring S [ L : R ] ; Iterating over S [ L ] to S [ R ] ; Incrementing the count of s [ i ] character in frequency array ; If frequency of any character is > 0 then increment the counter ; Driver code
def findCount ( s , L , R ) : NEW_LINE INDENT distinct = 0 NEW_LINE frequency = [ 0 for i in range ( 26 ) ] NEW_LINE for i in range ( L , R + 1 , 1 ) : NEW_LINE INDENT frequency [ ord ( s [ i ] ) - ord ( ' a ' ) ] += 1 NEW_LINE DEDENT for i in range ( 26 ) : NEW_LINE INDENT if ( frequency [ i ] > 0 ) : NEW_LINE INDENT distinct += 1 NEW_LINE DEDENT DEDENT print ( distinct ) NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT s = " geeksforgeeksisacomputerscienceportal " NEW_LINE queries = 3 NEW_LINE Q = [ [ 0 , 10 ] , [ 15 , 18 ] , [ 12 , 20 ] ] NEW_LINE for i in range ( queries ) : NEW_LINE INDENT findCount ( s , Q [ i ] [ 0 ] , Q [ i ] [ 1 ] ) NEW_LINE DEDENT DEDENT