Watch 10 video solutions for Patients With a Condition, a easy level problem involving Database. This walkthrough by Learn With Chirag has 4,957 views views. Want to try solving it yourself? Practice on FleetCode or read the detailed text solution.
Table: Patients
+--------------+---------+ | Column Name | Type | +--------------+---------+ | patient_id | int | | patient_name | varchar | | conditions | varchar | +--------------+---------+ patient_id is the primary key (column with unique values) for this table. 'conditions' contains 0 or more code separated by spaces. This table contains information of the patients in the hospital.
Write a solution to find the patient_id, patient_name, and conditions of the patients who have Type I Diabetes. Type I Diabetes always starts with DIAB1 prefix.
Return the result table in any order.
The result format is in the following example.
Example 1:
Input: Patients table: +------------+--------------+--------------+ | patient_id | patient_name | conditions | +------------+--------------+--------------+ | 1 | Daniel | YFEV COUGH | | 2 | Alice | | | 3 | Bob | DIAB100 MYOP | | 4 | George | ACNE DIAB100 | | 5 | Alain | DIAB201 | +------------+--------------+--------------+ Output: +------------+--------------+--------------+ | patient_id | patient_name | conditions | +------------+--------------+--------------+ | 3 | Bob | DIAB100 MYOP | | 4 | George | ACNE DIAB100 | +------------+--------------+--------------+ Explanation: Bob and George both have a condition that starts with DIAB1.
Problem Overview: The table contains patient records with a conditions column storing multiple condition codes separated by spaces. The task is to return patients whose condition list contains the code DIAB1 as a standalone code, not as part of another code like DIAB100.
Approach 1: String Matching with Split Function (O(n * m) time, O(m) space)
Treat the conditions column as a space-separated list of codes. Split the string into tokens and check whether one of them equals DIAB1. The algorithm iterates through each patient record, performs a split(" ") operation, then scans the resulting list for the exact code. The key insight is avoiding substring matches like DIAB100 by comparing full tokens only. This approach is straightforward and works well when processing rows in application logic using languages like Python or JavaScript. Time complexity is O(n * m) where n is the number of rows and m is the average number of condition codes per row, with O(m) temporary space for the token list. This technique relies on basic string processing.
Approach 2: Using Regular Expressions (O(n * m) time, O(1) space)
Use a regular expression that matches DIAB1 as a full word inside the string. The pattern checks either the start of the string or a preceding space, followed by DIAB1, and ensures the code ends before another character continues the token. In SQL or backend filtering logic, a regex like (^|\s)DIAB1(\s|$) guarantees correct boundaries. Each row is scanned once by the regex engine, so the complexity remains O(n * m), but it avoids allocating intermediate arrays. This approach is cleaner when regex support is available in languages like Java or C#. It directly expresses the boundary condition and is common in database-style filtering problems involving regular expressions and SQL-style pattern matching.
Recommended for interviews: The regex approach is typically preferred because it expresses the "standalone code" requirement precisely and keeps the query concise. However, explaining the split-based method first shows you understand the underlying structure of the data and the risk of partial matches. Strong candidates often mention both approaches: tokenization for clarity and regex for compact production queries.
| Approach | Time | Space | When to Use |
|---|---|---|---|
| String Matching with Split Function | O(n * m) | O(m) | When processing rows in application code and you want explicit token comparison. |
| Regular Expression Matching | O(n * m) | O(1) | When regex support is available and you want concise pattern matching for word boundaries. |