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 ; To display the result
function minEmail ( email ) { let ans = " " ; let len = email . length ; ans += email [ 0 ] ; let i = 1 ; let notAt = true ; while ( i < len ) { if ( i < len - 3 && notAt && email [ i ] == ' ' && email [ i + 1 ] == ' ' ) { ans += ' ' ; i += 1 ; notAt = false ; } else if ( i < len - 4 && email [ i ] == ' ' && email [ i + 1 ] == ' ' && email [ i + 2 ] == ' ' ) { ans += ' ' ; i += 2 ; } else { ans += email [ i ] ; } i += 1 ; } return ans ; } let email = " " ; document . write ( minEmail ( email ) ) ;