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.
1function getPatientsWithDiab1(patients) {
2
This JavaScript function getPatientsWithDiab1
filters the input list of patient records. For each patient, it splits the 'conditions' string and checks if any condition code starts with 'DIAB1'. If found, the patient record is included in the result.
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.