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.
This approach involves iterating over each row in the Patients table. We split the 'conditions' string by spaces and check if any of the resulting parts starts with the prefix 'DIAB1'. If so, we include that patient in our result set.
The solution defines a function get_patients_with_diab1 that takes a list of tuples as input, where each tuple represents a row from the Patients table. It iterates over each patient, splits the 'conditions' string into a list of condition codes, and checks if any of those conditions start with 'DIAB1'. If a match is found, the patient's details are added to the result list.
Python
JavaScript
Time Complexity: O(n * m), where n is the number of patients and m is the average number of conditions per patient.
Space Complexity: O(n) due to the space needed to store the result list.
This approach utilizes regular expressions to find patients with conditions starting with 'DIAB1'. It iterates over each patient's 'conditions' and uses a regex pattern to perform the check.
This Java solution uses a regular expression to match any condition that starts with 'DIAB1'. The regex pattern is \\bDIAB1\\d*, where \\b ensures that the match occurs at a word boundary, and \\d* allows for any digits following 'DIAB1'. For each patient, the matcher checks for this pattern in the conditions string. If found, the patient's details are added to the result list.
Time Complexity: O(n * m), where n is the number of patients and m is the length of the conditions string.
Space Complexity: O(n), proportional to the result list size.
MySQL
| Approach | Complexity |
|---|---|
| Approach 1: String Matching with Split Function | Time Complexity: O(n * m), where n is the number of patients and m is the average number of conditions per patient. Space Complexity: O(n) due to the space needed to store the result list. |
| Approach 2: Using Regular Expressions | Time Complexity: O(n * m), where n is the number of patients and m is the length of the conditions string. Space Complexity: O(n), proportional to the result list size. |
| Default Approach | — |
| 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. |
Patients With a Condition | Leetcode 1527 | Crack SQL Interviews in 50 Qs #mysql #leetcode • Learn With Chirag • 4,957 views views
Watch 9 more video solutions →Practice Patients With a Condition with our built-in code editor and test cases.
Practice on FleetCode