{x}
blog image

Patients With a Condition

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.

Solution Explanation for LeetCode Problem 1527: Patients With a Condition

This problem requires querying a database table (Patients) to find patients with a specific condition. The condition is identified by a string prefix "DIAB1" within the conditions column. The solution uses SQL to efficiently perform this task.

Approach

The core idea is to utilize SQL's LIKE operator with wildcard characters (%) to search for the "DIAB1" prefix within the conditions column. Since the "DIAB1" prefix might appear at the beginning, in the middle, or at the end of the conditions string (separated by spaces), we need two LIKE conditions in the WHERE clause to cover all possibilities.

SQL Solution (MySQL)

SELECT
    patient_id,
    patient_name,
    conditions
FROM patients
WHERE conditions LIKE 'DIAB1%' OR conditions LIKE '% DIAB1%';

Explanation:

  • SELECT patient_id, patient_name, conditions: This selects the desired columns from the Patients table.
  • FROM patients: This specifies the table to query.
  • WHERE conditions LIKE 'DIAB1%' OR conditions LIKE '% DIAB1%': This is the crucial part.
    • conditions LIKE 'DIAB1%': This condition finds rows where the conditions column starts with "DIAB1" followed by any characters.
    • conditions LIKE '% DIAB1%': This condition finds rows where the conditions column contains "DIAB1" anywhere within the string, preceded and followed by any characters (including spaces). The space before DIAB1 is crucial to avoid matching cases like "DIAB100" when searching for "DIAB1".
  • OR: The OR operator combines the two LIKE conditions, ensuring that rows matching either condition are included in the result.

Time Complexity Analysis

The time complexity of this SQL query is primarily determined by the database's query processing engine. In general, a LIKE query with a prefix search can be optimized using indexes if an index exists on the conditions column. With an index, the time complexity would be close to O(log n) for searching (where n is the number of rows in the Patients table) due to the efficiency of index lookups. However, without an index, a full table scan would be required, leading to a time complexity of O(n).

The OR condition does not significantly alter the time complexity because the database optimizer can efficiently process both LIKE conditions.

Space Complexity Analysis

The space complexity is O(k), where k is the size of the output (the number of rows satisfying the condition multiplied by the size of each row). The space needed to store the output depends on the number of patients who have "DIAB1" in their conditions. The space used for processing the query by the database system is generally considered constant relative to the input size.