Reduce a string to a valid email address of minimum length by replacing specified substrings | Function to find the minimum length by replacing at with @ and dot with ' . ' such that the string is valid email ; Stores string by replacing at with @ and dot with ' . ' such that the string is valid email ; append first character ; Stores index ; Check if at ( @ ) already included or not ; Iterate over characters of the string ; at can be replaced at most once ; Update ans ; Update i ; Update notAt ; If current substring found dot ; Update ans ; Update i ; Update ans ; Update i ; Driver Code ; To display the result
def minEmail ( email ) : NEW_LINE INDENT ans = ' ' NEW_LINE ans += email [ 0 ] NEW_LINE i = 1 NEW_LINE notAt = True NEW_LINE while i < len ( email ) : NEW_LINE INDENT if ( i < len ( email ) - 3 and notAt and email [ i : i + 2 ] == ' at ' ) : NEW_LINE INDENT ans += ' @ ' NEW_LINE i += 1 NEW_LINE notAt = False NEW_LINE DEDENT elif i < len ( email ) - 4 and email [ i : i + 3 ] == ' dot ' : NEW_LINE INDENT ans += ' . ' NEW_LINE i += 2 NEW_LINE DEDENT else : NEW_LINE INDENT ans += email [ i ] NEW_LINE DEDENT i += 1 NEW_LINE DEDENT return ans NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT email = ' geeksforgeeksatgmaildotcom ' NEW_LINE print ( minEmail ( email ) ) NEW_LINE DEDENT