FastPrepValidate Email Address

Validate Email Address

Accenture logoAccenture● EasyNEW GRADOA
Learn

Problem statement

Given a string email, return true when it satisfies all of these rules and false otherwise:

  • It contains exactly one @.
  • The local part before @ is nonempty and contains only lowercase letters, digits, dots, underscores, and hyphens.
  • The domain after @ contains at least two nonempty labels separated by dots.
  • Each domain label contains only lowercase letters, digits, and hyphens and cannot begin or end with a hyphen.

This exercise defines a bounded validation grammar; it is not full RFC email validation.

Function

isValidEmail(email: String) → boolean

Examples

Example 1

email = "dev.team@example.com"return = true

The address has one @, a valid local part, and two valid domain labels.

Example 2

email = "userexample.com"return = false

The address does not contain an @.

Example 3

email = "user@-example.com"return = false

The first domain label begins with a hyphen.

Constraints

  • 1 <= email.length <= 10^5.
  • email contains ASCII characters.

More Accenture problems

See Accenture hiring insights
public boolean isValidEmail(String email) {
    // Write your code here.
}
email"dev.team@example.com"
expectedtrue
Checking account…