#1517

Find Users With Valid E-Mails

Easy
DatabaseRegexString Matching
LeetCode ↗

Approaches

Brute ForceOptimal
Complexity Comparison
Brute ForceOptimal Solution
Time
O(n²)
O(n)
Space
O(1)
O(n)
💡

Intuition

Time O(n)Space O(n)

The optimal solution leverages SQL's pattern matching capabilities to efficiently filter valid emails in a single query, reducing the need for multiple checks.

⚙️

Algorithm

3 steps
  1. 1Step 1: Use a SQL query to select users whose emails end with '@leetcode.com'.
  2. 2Step 2: Use a regular expression to ensure the prefix starts with a letter and contains only valid characters.
  3. 3Step 3: Return the user_id, name, and mail of valid users.
solution.py1 lines
1SELECT user_id, name, mail FROM Users WHERE mail LIKE '%@leetcode.com' AND mail REGEXP '^[A-Za-z][A-Za-z0-9_.-]*@leetcode.com';

Complexity note: The time complexity is O(n) because we are scanning through the list of users once, applying the regex and LIKE conditions in a single pass.

  • 1Emails must end with '@leetcode.com'.
  • 2The prefix must start with a letter and contain valid characters.

Solutions and explanations are original Tejav content. Problem titles © LeetCode — use the LeetCode button above for the full problem statement.