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.
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.
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.
C#
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.
| 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. |
LeetCode 1527 "Patients With a Condition" Interview SQL Question with Detailed Explanation • Everyday Data Science • 3,022 views views
Watch 9 more video solutions →Practice Patients With a Condition with our built-in code editor and test cases.
Practice on FleetCode