Problem 1517

Abstract / Summary

Find users whose email addresses follow the correct format rules.

Note

The full problem description, including helpful examples, can be found on LeetCode.

MySQL 1517.sql

SELECT user_id, name, mail
  FROM Users
 WHERE mail REGEXP '^[a-zA-Z][a-zA-Z0-9._-]*[@]leetcode[.]com$'
3 lines | 107 bytes | submitted September 7, 2023 | GitHub  

Difficulty

This problem is ranked as
easy medium hard

and has an acceptance rate of about 36% (800k+ submissions).

Typical patterns to solve this problem:
  • Database
I solved 663 easy, 1250 medium and 309 hard problems

AI Disclaimer

Every single line of code was written by me, 100% from scratch, between August 2023 and August 2024.
However, two years later I set up this website and used various AI tools to generate most of the walkthrough / explanations below.

See this dedicated page for more details.
LeetCode profile | GitHub repo

Code Walkthrough

Overview

The provided SQL statement extracts three columns-user_id, name, and mail-from the Users table, restricting the result set to rows whose mail field conforms to a specific regular-expression pattern. The pattern enforces a strict format for email addresses belonging to the leetcode.com domain, ensuring that the local part begins with an alphabetic character and contains only permitted symbols thereafter.

Key Steps and Corresponding Snippets

The query can be broken down into three logical components: column selection, source specification, and row filtering.

The column selection is expressed by the SELECT clause:

SELECT user_id, name, mail

The source of the data is identified by the FROM clause:

FROM Users

Row filtering is performed by the WHERE clause that applies a regular-expression predicate:

WHERE mail REGEXP '^[a-zA-Z][a-zA-Z0-9._-]*[@]leetcode[.]com$'

Together, these fragments compose the full statement:

SELECT user_id, name, mail
  FROM Users
 WHERE mail REGEXP '^[a-zA-Z][a-zA-Z0-9._-]*[@]leetcode[.]com$'

Edge Cases Handled by the Regular Expression

The regular expression ^[a-zA-Z][a-zA-Z0-9._-]*[@]leetcode[.]com$ is anchored at both the start (^) and end ($) of the string, guaranteeing that the entire mail value must match the pattern without any leading or trailing characters.

The first character class [a-zA-Z] ensures that the local part cannot be empty and must start with an alphabetic character, thereby rejecting addresses that begin with a digit, underscore, hyphen, or period.

The subsequent character class [a-zA-Z0-9._-]* permits any combination of letters, digits, periods, underscores, and hyphens after the initial letter. Because the quantifier * allows zero or more occurrences, the local part may consist of a single letter followed directly by the domain separator.

The literal [@] matches the @ symbol explicitly, preventing accidental inclusion of similar characters.

The domain portion leetcode[.]com is fixed to the exact string leetcode.com. The use of [.] instead of a plain dot avoids any special meaning the dot might have in other regex contexts, ensuring that only a literal period is accepted.

By anchoring the pattern, the query excludes addresses that contain additional subdomains (e.g., mail@sub.leetcode.com) or extra characters after the domain (e.g., mail@leetcode.com.org). It also rejects case variations in the domain because the pattern specifies lowercase letters only for leetcode.com.

Performance Optimizations Observed

The regular expression is deliberately constructed to be deterministic and linear. The initial anchor ^ combined with a single mandatory alphabetic character eliminates the need for backtracking at the start of the string. The subsequent character class [a-zA-Z0-9._-]* uses a simple repetition without nested alternations, allowing the regex engine to process the local part in a single pass.

The final anchor $ ensures that the engine can stop scanning as soon as the domain portion is matched, avoiding unnecessary checks beyond the end of the string. By limiting the pattern to a fixed domain literal, the engine does not need to evaluate complex alternatives or lookaheads, which contributes to faster evaluation.

Because the pattern is anchored and does not contain costly constructs such as greedy quantifiers over ambiguous subpatterns, the database can often apply the predicate efficiently, especially when combined with an index on the mail column or a functional index that stores the result of the regex evaluation. This design minimizes full-table scans and leverages the database’s ability to short-circuit evaluation when a row fails early criteria.

written by openai gpt

ELI5 - Simplified Explanation

Imagine the computer has a giant treasure box filled with files about every person who visits a website. Each file has a name, a user identification number, and an email address written on it. This piece of code is just a way to go through all those files to pick out only the ones that match a special pattern. It looks through the stack and grabs the ID, the name, and the email for certain users. The complicated part at the end acts like a filter to make sure the email address follows a specific set of rules. It is checking to ensure the address starts with a letter and finishes exactly with at leetcode dot com. If the file follows that exact rule, the computer keeps it, but if it does not match, the computer just ignores it and keeps looking at the next file in the box.

written by google gemini

Code Quality

Several LLMs ranked my code quality on a scale from 1 (very poor) to 10 (outstanding):
n = 27 | median = 7 | μ = 6.85 | σ = 1.46

Complexity Analysis

not analyzed yet

Other Solutions

I wrote a basic crawler to find other solutions to that problem.
Obviously, I did that after I solved the problem myself.

In general, AlgoMonster and WalkCCC solutions are high-quality and always worth a look.
I haven't checked most of the other solutions, but they might be helpful as well.

And of course, the LeetCode forum is always a good place (or even the best) to look for alternative approaches.

(click on a column header to sort the table)
author
author solutions
www AlgoMonster C++ Java Python
WalkCCC SQL
leetcode.ca Python SQL
Progiez C++ Java Python
AlgoMonster C++, Python, Java
WalkCCC SQL
leetcode.ca Python, SQL
Progiez C++, Java, Python
9 links | 4 authors | 4 languages

Videos

5 videos   did my crawler come up with invalid videos ? drop me a line.

Solved On The Same Day

29 easy 13 medium
43 solutions   September 7, 2023

Extra

There are a few extra pages giving you some insights into my setup:
  • Tech Stack - my development environment and tools
  • AI - how AI helped me create this website (NOT solving the problems)
  • Progress - progression over time
  • Quality - code quality