Patients With a Condition — LeetCode 1527 Python Solution

EasyDatabase
Problem
#1527
Reading time
2 min

Table schema

SQL
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.

Example

SQL
+--------------+---------+
| 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.

Python solution

Python
import duckdb
import pandas as pd

def solution(patients: pd.DataFrame) -> pd.DataFrame:
    con = duckdb.connect()
    con.register("Patients", patients)
    return con.execute("""SELECT
    patient_id,
    patient_name,
    conditions
FROM patients
WHERE conditions LIKE 'DIAB1%' OR conditions LIKE '% DIAB1%';""").df()

Complexity

MeasureComplexity
TimeO(n log n) (typical)
SpaceO(n) auxiliary

Related problems

Frequently asked questions

How hard is LeetCode 1527. Patients With a Condition?
LeetCode 1527. Patients With a Condition is rated Easy on LeetCode.
What topics does LeetCode 1527. Patients With a Condition cover?
LeetCode 1527. Patients With a Condition is tagged Database on LeetCode.

Stuck on problems like this in a live interview?

Stealth Interview is a desktop app for macOS and Windows. It reads the problem off your screen and returns a working solution with a step-by-step explanation and its time and space complexity — invisible to screen sharing.

Get Stealth Interview