Check if frequency of each character is equal to its position in English Alphabet | Python3 program for the above approach ; Initialise frequency array ; Traverse the string ; Update the frequency ; Check for valid string ; If frequency is non - zero ; If freq is not equals to ( i + 1 ) , then return false ; Return true ; Given string str
def checkValidString ( str ) : NEW_LINE INDENT freq = [ 0 for i in range ( 26 ) ] NEW_LINE for i in range ( len ( str ) ) : NEW_LINE INDENT freq [ ord ( str [ i ] ) - ord ( ' a ' ) ] += 1 NEW_LINE DEDENT for i in range ( 26 ) : NEW_LINE INDENT if ( freq [ i ] != 0 ) : NEW_LINE INDENT if ( freq [ i ] != i + 1 ) : NEW_LINE INDENT return False NEW_LINE DEDENT DEDENT DEDENT return True NEW_LINE DEDENT str = " abbcccdddd " NEW_LINE if ( checkValidString ( str ) ) : NEW_LINE INDENT print ( " Yes " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " No " ) NEW_LINE DEDENT