Length of longest subsequence whose difference between maximum and minimum ASCII value of characters is exactly one | Function to find the maximum length of subsequence having difference of ASCII value of longest and smallest character as 1 ; Stores frequency of characters ; Iterate over characters of the string ; Stores the resultant length of subsequence ; Check if there exists any elements with ASCII value one less than character ch ; Size of current subsequence ; Update the value of ans ; Print the resultant count ; Driver Code
def maximumLengthSubsequence ( str ) : NEW_LINE INDENT mp = { } NEW_LINE for ch in str : NEW_LINE INDENT if ch in mp . keys ( ) : NEW_LINE INDENT mp [ ch ] += 1 NEW_LINE DEDENT else : NEW_LINE INDENT mp [ ch ] = 1 NEW_LINE DEDENT DEDENT ans = 0 NEW_LINE for ch in str : NEW_LINE INDENT if chr ( ord ( ch ) - 1 ) in mp . keys ( ) : NEW_LINE INDENT curr_max = mp [ ch ] NEW_LINE if chr ( ord ( ch ) - 1 ) in mp . keys ( ) : NEW_LINE INDENT curr_max += mp [ chr ( ord ( ch ) - 1 ) ] NEW_LINE DEDENT ans = max ( ans , curr_max ) NEW_LINE DEDENT DEDENT print ( ans ) NEW_LINE DEDENT S = " acbbebcg " NEW_LINE maximumLengthSubsequence ( S ) NEW_LINE