Sponsored
Sponsored
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.
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.
1def get_patients_with_diab1(patients):
2 result
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.
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.
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.
1import java.util.*;
2import java.util.regex.*;
3
4public class Solution {
5 public List<int[]> getPatientsWithDiab1(List<String[]> patients) {
6 List<int[]> result = new ArrayList<>();
7 Pattern pattern = Pattern.compile("\\bDIAB1\\d*");
8 for (String[] patient : patients) {
9 String conditions = patient[2];
10 Matcher matcher = pattern.matcher(conditions);
11 if (matcher.find()) {
12 result.add(new int[]{Integer.parseInt(patient[0]), patient[1], patient[2]});
13 }
14 }
15 return result;
16 }
17}
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.