EasyNo. 1527

Patients With a Condition — LeetCode 1527 Python Solution

Topics
Database
Reading time
2 min

Table schema

SQLTable schema
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

SQLExample
+--------------+---------+
| 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

PythonSolution
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

Time
O(n log n)(typical)
Space · auxiliary
O(n)

Related problems

Related problems
No.ProblemDifficulty
0175Combine Two TablesEasy
0176Second Highest SalaryMedium
0177Nth Highest SalaryMedium
0178Rank ScoresMedium
0180Consecutive NumbersMedium
0181Employees Earning More Than Their ManagersEasy

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.

Free to download. Check it runs on your machine, then pick a plan.