question_slug
stringlengths
3
77
title
stringlengths
1
183
slug
stringlengths
12
45
summary
stringlengths
1
160
author
stringlengths
2
30
certification
stringclasses
2 values
created_at
stringdate
2013-10-25 17:32:12
2025-04-12 09:38:24
updated_at
stringdate
2013-10-25 17:32:12
2025-04-12 09:38:24
hit_count
int64
0
10.6M
has_video
bool
2 classes
content
stringlengths
4
576k
upvotes
int64
0
11.5k
downvotes
int64
0
358
tags
stringlengths
2
193
comments
int64
0
2.56k
patients-with-a-condition
🔥💯 Pandas || MySQL: An Effortless and Simple Approach! (With Comments and Explanation) 🔥💯
pandas-mysql-an-effortless-and-simple-ap-bejo
Pandas Approach\n- Use the str.contains() method to find patients with Type I Diabetes:\n\npatients_with_diabetes = patients[patients[\'conditions\'].str.contai
sriganesh777
NORMAL
2023-08-02T16:04:15.608189+00:00
2023-08-02T16:04:15.608210+00:00
21,058
false
# Pandas Approach\n- Use the str.contains() method to find patients with Type I Diabetes:\n```\npatients_with_diabetes = patients[patients[\'conditions\'].str.contains(r\'\\bDIAB1\')]\n\n```\nThe str.contains() method with the regex pattern r\'\\bDIAB1\' checks each entry in the \'conditions\' column for the presence of \'DIAB1\'. The \\b in the pattern is a word boundary assertion that ensures \'DIAB1\' is a separate word and not part of another word. This ensures that we only get patients with Type I Diabetes and not other conditions that might contain \'DIAB1\' as part of the word.\n\n- Select only the required columns in the result DataFrame:\n```\n result_df = patients_with_diabetes[[\'patient_id\', \'patient_name\', \'conditions\']]\n```\nThe result_df DataFrame contains only the \'patient_id\', \'patient_name\', and \'conditions\' columns for patients with Type I Diabetes.\n\n# Pandas Code\n```\nimport pandas as pd\n\ndef find_patients(patients: pd.DataFrame) -> pd.DataFrame:\n # Use the str.contains() method to find patients with Type I Diabetes\n patients_with_diabetes = patients[patients[\'conditions\'].str.contains(r\'\\bDIAB1\')]\n \n # Select only the required columns\n result_df = patients_with_diabetes[[\'patient_id\', \'patient_name\', \'conditions\']]\n \n return result_df\n```\n# MySQL Query\n```\nSELECT patient_id, patient_name, conditions\nFROM Patients\nWHERE conditions LIKE \'DIAB1%\' OR conditions LIKE \'% DIAB1%\';\n```\n# Explanation\n- `SELECT patient_id, patient_name, conditions:` This part of the query selects the columns \'patient_id\', \'patient_name\', and \'conditions\' from the \'Patients\' table.\n\n- `FROM Patients:` This specifies the table from which we are selecting the data, which is the \'Patients\' table in this case.\n\n- `WHERE conditions LIKE \'DIAB1%\' OR conditions LIKE \'% DIAB1%\':` This is the condition for filtering the rows. The query retrieves rows from the \'Patients\' table that meet either of the two conditions:\n\n1. `conditions LIKE \'DIAB1%\':` This condition matches rows where the \'conditions\' column starts with the string \'DIAB1\'. The % wildcard is used to match any sequence of characters after \'DIAB1\'. So, this part of the condition will match rows where \'conditions\' is exactly \'DIAB1\' or starts with \'DIAB1\' followed by any characters.\n\n2. `conditions LIKE \'% DIAB1%\':` This condition matches rows where the \'conditions\' column contains the string \' DIAB1\'. The % wildcard at the beginning allows for any characters before \' DIAB1\', and the % wildcard at the end allows for any characters after \' DIAB1\'. This part of the condition will match rows where \'conditions\' contains \' DIAB1\' as a separate word, with spaces before and after it.\n\nBy using the OR operator between the two conditions, the query retrieves rows that meet either of these conditions. It will return rows where the \'conditions\' column starts with \'DIAB1\' or contains \' DIAB1\' as a separate word.\n\nIn summary, the SQL query selects the \'patient_id\', \'patient_name\', and \'conditions\' columns from the \'Patients\' table for patients who have Type I Diabetes. Type I Diabetes is identified based on the condition that the \'conditions\' column either starts with \'DIAB1\' or contains \' DIAB1\' as a separate word.\n![upvote img.jpg](https://assets.leetcode.com/users/images/6de40c20-572d-4041-8d61-564eca15fcf3_1690992168.1768682.jpeg)\n
117
0
['MySQL', 'Pandas']
13
patients-with-a-condition
2 DIFFERENT SOLUTIONS | Efficient solution
2-different-solutions-efficient-solution-6fah
Soltion 1\n\nSELECT * FROM patients WHERE conditions REGEXP \'\\\\bDIAB1\'\n\n\n\nSolution 2 | Efficient\n\nSELECT *\nFROM Patients\nWHERE conditions LIKE \'% D
patil_pratik
NORMAL
2022-06-09T08:47:44.685919+00:00
2022-06-09T08:47:44.685946+00:00
15,404
false
Soltion 1\n```\nSELECT * FROM patients WHERE conditions REGEXP \'\\\\bDIAB1\'\n```\n\n\nSolution 2 | Efficient\n```\nSELECT *\nFROM Patients\nWHERE conditions LIKE \'% DIAB1%\' OR conditions LIKE \'DIAB1%\'\n```\n\n
82
0
['MySQL']
6
patients-with-a-condition
Simple solution just using 'LIKE' clause with 92% beats
simple-solution-just-using-like-clause-w-h6yr
\n\n# Code\nmysql []\n# Write your MySQL query statement below\n\nselect patient_id,patient_name,conditions from Patients\nwhere conditions like \'DIAB1%\' or
mantosh_kumar04
NORMAL
2024-09-22T11:11:21.550568+00:00
2024-09-22T11:13:40.669931+00:00
12,718
false
\n\n# Code\n```mysql []\n# Write your MySQL query statement below\n\nselect patient_id,patient_name,conditions from Patients\nwhere conditions like \'DIAB1%\' or conditions like \'% DIAB1%\' ;\n```\n![upvote image leet.jpeg](https://assets.leetcode.com/users/images/d0a1115c-dcf9-499f-ab46-7fb9c341c6de_1727003476.1171367.jpeg)\n
67
0
['MySQL']
2
patients-with-a-condition
Very easy solution
very-easy-solution-by-im_obid-lx1t
\n# Code\n\nselect * from patients \nwhere conditions like \'% DIAB1%\' \nor conditions like \'DIAB1%\';\n
im_obid
NORMAL
2023-01-06T17:44:45.742394+00:00
2023-01-06T17:44:45.742440+00:00
11,303
false
\n# Code\n```\nselect * from patients \nwhere conditions like \'% DIAB1%\' \nor conditions like \'DIAB1%\';\n```
23
0
['MySQL']
1
patients-with-a-condition
Pandas, Polars, PostgreSQL
pandas-polars-postgresql-by-speedyy-n6la
Intuition\n- After solving this problem now solve Find Users With Valid E-Mails.\njs\nr\'\\bDIAB1\' -> This pattern doesn\'t work, why?\n\nWhat \'\\b\' means?
speedyy
NORMAL
2024-11-29T10:15:06.382418+00:00
2024-12-08T12:43:24.326280+00:00
1,977
false
# Intuition\n- After solving this problem now solve [Find Users With Valid E-Mails](https://leetcode.com/problems/find-users-with-valid-e-mails/description/).\n```js\nr\'\\bDIAB1\' -> This pattern doesn\'t work, why?\n\nWhat \'\\b\' means? Word Boundary. E.g. \'abc DIAB100\'\n |\n Just like Any Character is Separated from its Left and Right Characters with a \'Thin Line\'. Between that \n Space(\' \') and the character \'D\', \' D\', there is also a thin line that separated them. And this Thin_Line\n /Position before and after an entire Word is \'Word Boundary\' i.e. \'\\b\'.\n\nA Word Character, \'\\w\' = [A-Za-z0-9_] and a Non-Word Character(\'\\W\') is anything else(+, -, * or whitespace etc)\nA Word comprised Word Characters in terms of Regex. Since Space is a Non Word Character, lets define \'\\b\' again,\n\n\'\\b\' matches the position between word characters(\'WORD\') and a non-word character(\' \', \'+\'..). So :\n```\n![image.png](https://assets.leetcode.com/users/images/783823c9-fd52-46cb-b6ed-10014f8d1fb0_1732877377.1553018.png)\n\n```js\n(2) doesnt feel right because \'+DIAB100\' is a word to us(not usa) but not to regex. It was the definition, \'\\b\' is\nthe Position between a Word and a Non-Word Character(\' \', \'+\' ..). So r\'\\bDIAB1\' will match for (2) also.\n\nTo simplify, think every Non-Word Character as a Space, \' \'. So \n\n \'+DIAB100\' = \' DIAB100\'\n\nAnd \' DIAM100\' is True for r\'\\bDIAB1\'.\n\nNote : For any wrong information I am deeply sorry and feel free to correct me please.\n```\n```js\nSo we cant use \'\\b\' here, at least I couldnt since \'\\b\' matched even with \'+DIAB100\' according to regex.\n\n\'DIAB1\' must be at the Start of the Sentance OR at the Start of a Word(having a Whitespace before that Word)\n \'^DIAB1\' | \' DIAB1\'\n\n = r\'^DIAB1| DIAB1\' (2 DIFFERENT CONDITIONS SEPARTED BY \'|\')\n = r\'(^| )DIAB1\' (can also be written like this, one concise pattern)\n\n----------------------------------------------------------------------------------------------------------------\n\nr\'(^| )DIAB1\' : (By chatgpt)\n 1. Matches either the start of the string(^) OR(|) a single whitespace character(\' \') before DIAB1.\n 2. Parentheses \'()\' group the patterns together, making the \'OR\' apply only to \'^\' and \' \'.\n\nLets say \'ACNE DIAB100\' : for the first character(\'A\'), \'(^| )\' will be checked At First. If its True, then the\nregex engine will proceed to check the literal string \'DIAB1\'.\n\n---------------------------------------------------------------------------------------------------------------\n\nWait why not \'[^ ]\' instead of \'(^| )\' since \'^\' and \' \' are just Single Characters?\n`[^ ]`: Matches any character that is Not a Space. \'^\' is Meta Character, not a Literal String.\n This is a negated character class. It looks for one character that is NOT a space immediately before DIAB1.\n```\n- In Pandas and Polars `str.contains()` used `to check if the string CONTAINS a substring that matches a pattern.` which is suitable for us because we are not evaluating our regex expression on the whole string value, from the VERY FIRST CHARACTER, e.g. to check if the string value is in a perticular pattern or not.\n# Pandas\n```js []\nimport pandas as pd\n\ndef find_patients(patients: pd.DataFrame) -> pd.DataFrame:\n return patients.loc[ patients[\'conditions\'].str.contains(r\'(^| )DIAB1\') ]\n```\n\n# Polars\n```js []\nimport polars as pl\nfrom polars import col\n\ndef find_patients(patients: pl.LazyFrame) -> pl.LazyFrame:\n return patients.filter( col(\'conditions\').str.contains(r"(^| )DIAB1") )\n\nfind_patients(pl.LazyFrame(patients)) .collect()\n```\n\n# PostgreSQL (Execution Order : )\n- For checking any regex expression which returns True or False, we have `~` operator in PostgreSQL. [Read this](https://www.w3resource.com/PostgreSQL/postgresql-similar-operator.php)\n```postgresql []\n-- Write your PostgreSQL query statement below\nSELECT *\nFROM patients\nWHERE conditions ~ \'(^| )DIAB1\'; -- \'(^| )DIAB1\' is Raw String like Pandas. \n```
20
0
['PostgreSQL', 'Pandas']
5
patients-with-a-condition
Pandas vs SQL | Elegant & Short | All 30 Days of Pandas solutions ✅
pandas-vs-sql-elegant-short-all-30-days-9bkdn
Complexity\n- Time complexity: O(n)\n- Space complexity: O(n)\n\n# Code\nPython []\ndef find_patients(patients: pd.DataFrame) -> pd.DataFrame:\n return patie
Kyrylo-Ktl
NORMAL
2023-08-01T17:08:50.270410+00:00
2023-08-06T16:50:53.898418+00:00
3,033
false
# Complexity\n- Time complexity: $$O(n)$$\n- Space complexity: $$O(n)$$\n\n# Code\n```Python []\ndef find_patients(patients: pd.DataFrame) -> pd.DataFrame:\n return patients[\n patients[\'conditions\'].str.contains(r\'(^DIAB1)|( DIAB1)\')\n ]\n```\n```SQL []\nSELECT *\n FROM Patients\n WHERE conditions REGEXP \'\\\\bDIAB1\';\n```\n\n# Important!\n###### If you like the solution or find it useful, feel free to **upvote** for it, it will support me in creating high quality solutions)\n\n# 30 Days of Pandas solutions\n\n### Data Filtering \u2705\n- [Big Countries](https://leetcode.com/problems/big-countries/solutions/3848474/pandas-elegant-short-1-line/)\n- [Recyclable and Low Fat Products](https://leetcode.com/problems/recyclable-and-low-fat-products/solutions/3848500/pandas-elegant-short-1-line/)\n- [Customers Who Never Order](https://leetcode.com/problems/customers-who-never-order/solutions/3848527/pandas-elegant-short-1-line/)\n- [Article Views I](https://leetcode.com/problems/article-views-i/solutions/3867192/pandas-elegant-short-1-line/)\n\n\n### String Methods \u2705\n- [Invalid Tweets](https://leetcode.com/problems/invalid-tweets/solutions/3849121/pandas-elegant-short-1-line/)\n- [Calculate Special Bonus](https://leetcode.com/problems/calculate-special-bonus/solutions/3867209/pandas-elegant-short-1-line/)\n- [Fix Names in a Table](https://leetcode.com/problems/fix-names-in-a-table/solutions/3849167/pandas-elegant-short-1-line/)\n- [Find Users With Valid E-Mails](https://leetcode.com/problems/find-users-with-valid-e-mails/solutions/3849177/pandas-elegant-short-1-line/)\n- [Patients With a Condition](https://leetcode.com/problems/patients-with-a-condition/solutions/3849196/pandas-elegant-short-1-line-regex/)\n\n\n### Data Manipulation \u2705\n- [Nth Highest Salary](https://leetcode.com/problems/nth-highest-salary/solutions/3867257/pandas-elegant-short-1-line/)\n- [Second Highest Salary](https://leetcode.com/problems/second-highest-salary/solutions/3867278/pandas-elegant-short/)\n- [Department Highest Salary](https://leetcode.com/problems/department-highest-salary/solutions/3867312/pandas-elegant-short-1-line/)\n- [Rank Scores](https://leetcode.com/problems/rank-scores/solutions/3872817/pandas-elegant-short-1-line-all-30-days-of-pandas-solutions/)\n- [Delete Duplicate Emails](https://leetcode.com/problems/delete-duplicate-emails/solutions/3849211/pandas-elegant-short/)\n- [Rearrange Products Table](https://leetcode.com/problems/rearrange-products-table/solutions/3849226/pandas-elegant-short-1-line/)\n\n\n### Statistics \u2705\n- [The Number of Rich Customers](https://leetcode.com/problems/the-number-of-rich-customers/solutions/3849251/pandas-elegant-short-1-line/)\n- [Immediate Food Delivery I](https://leetcode.com/problems/immediate-food-delivery-i/solutions/3872719/pandas-elegant-short-1-line-all-30-days-of-pandas-solutions/)\n- [Count Salary Categories](https://leetcode.com/problems/count-salary-categories/solutions/3872801/pandas-elegant-short-1-line-all-30-days-of-pandas-solutions/)\n\n\n### Data Aggregation \u2705\n- [Find Total Time Spent by Each Employee](https://leetcode.com/problems/find-total-time-spent-by-each-employee/solutions/3872715/pandas-elegant-short-1-line-all-30-days-of-pandas-solutions/)\n- [Game Play Analysis I](https://leetcode.com/problems/game-play-analysis-i/solutions/3863223/pandas-elegant-short-1-line/)\n- [Number of Unique Subjects Taught by Each Teacher](https://leetcode.com/problems/number-of-unique-subjects-taught-by-each-teacher/solutions/3863239/pandas-elegant-short-1-line/)\n- [Classes More Than 5 Students](https://leetcode.com/problems/classes-more-than-5-students/solutions/3863249/pandas-elegant-short/)\n- [Customer Placing the Largest Number of Orders](https://leetcode.com/problems/customer-placing-the-largest-number-of-orders/solutions/3863257/pandas-elegant-short-1-line/)\n- [Group Sold Products By The Date](https://leetcode.com/problems/group-sold-products-by-the-date/solutions/3863267/pandas-elegant-short-1-line/)\n- [Daily Leads and Partners](https://leetcode.com/problems/daily-leads-and-partners/solutions/3863279/pandas-elegant-short-1-line/)\n\n\n### Data Aggregation \u2705\n- [Actors and Directors Who Cooperated At Least Three Times](https://leetcode.com/problems/actors-and-directors-who-cooperated-at-least-three-times/solutions/3863309/pandas-elegant-short/)\n- [Replace Employee ID With The Unique Identifier](https://leetcode.com/problems/replace-employee-id-with-the-unique-identifier/solutions/3872822/pandas-elegant-short-1-line-all-30-days-of-pandas-solutions/)\n- [Students and Examinations](https://leetcode.com/problems/students-and-examinations/solutions/3872699/pandas-elegant-short-1-line-all-30-days-of-pandas-solutions/)\n- [Managers with at Least 5 Direct Reports](https://leetcode.com/problems/managers-with-at-least-5-direct-reports/solutions/3872861/pandas-elegant-short/)\n- [Sales Person](https://leetcode.com/problems/sales-person/solutions/3872712/pandas-elegant-short-1-line-all-30-days-of-pandas-solutions/)\n\n
19
0
['Python', 'Python3', 'MySQL', 'Pandas']
2
patients-with-a-condition
Smart LOGIC MYSQL
smart-logic-mysql-by-ganjinaveen-fe1q
\n\n# Smart Logic MYSQL\n\nselect * from patients where conditions like "% DIAB1%" or\nconditions like "DIAB1%";\n\n# please upvote me it would encourage me alo
GANJINAVEEN
NORMAL
2023-04-17T12:54:58.244012+00:00
2023-04-17T12:54:58.244056+00:00
5,310
false
\n\n# Smart Logic MYSQL\n```\nselect * from patients where conditions like "% DIAB1%" or\nconditions like "DIAB1%";\n```\n# please upvote me it would encourage me alot\n
19
0
['MySQL']
1
patients-with-a-condition
EASY WAY TO SOLVE THIS PROBLEM USING "LIKE"
easy-way-to-solve-this-problem-using-lik-hvof
IntuitionPLEASE UPVOTE IF YOU FIND IT HELPFULL! ApproachComplexity Time complexity: Space complexity: Code
sayandutta2002
NORMAL
2025-03-10T09:19:30.131136+00:00
2025-03-10T09:19:30.131136+00:00
2,970
false
# Intuition PLEASE UPVOTE IF YOU FIND IT HELPFULL! ![images.jpeg](https://assets.leetcode.com/users/images/402849fa-769e-46fa-ba3e-460eea621e29_1741598328.8248084.jpeg) <!-- Describe your first thoughts on how to solve this problem. --> # Approach <!-- Describe your approach to solving the problem. --> # Complexity - Time complexity: <!-- Add your time complexity here, e.g. $$O(n)$$ --> - Space complexity: <!-- Add your space complexity here, e.g. $$O(n)$$ --> # Code ```oraclesql [] /* Write your PL/SQL query statement below */ SELECT patient_id, patient_name, conditions FROM Patients WHERE conditions LIKE 'DIAB1%' OR conditions LIKE '% DIAB1%' ```
13
0
['Database', 'MySQL', 'Oracle', 'MS SQL Server', 'PostgreSQL']
0
patients-with-a-condition
👇 MySQL 2 solutions | very easy 🔥 regexp | no like
mysql-2-solutions-very-easy-regexp-no-li-7gyd
\uD83D\uDE4B\uD83C\uDFFB\u200D\u2640\uFE0F Hello, here are my solutions to the problem.\n### Please upvote to motivate me post future solutions. HAPPY CODING \u
rusgurik
NORMAL
2022-08-29T14:42:03.379291+00:00
2022-08-29T14:42:03.379341+00:00
1,740
false
### \uD83D\uDE4B\uD83C\uDFFB\u200D\u2640\uFE0F Hello, here are my solutions to the problem.\n### Please upvote to motivate me post future solutions. HAPPY CODING \u2764\uFE0F\n##### Any suggestions and improvements are always welcome.\n##### Solution 1: regexp, \'\\\\b...\' \uD83C\uDFAF\n##### \u2705 Runtime: 536 ms, faster than 31.19% of MySQL\n```\nselect *\nfrom patients\nwhere conditions regexp \'\\\\bDIAB1\'\n```\n\\b - word boundary so it checks that it starts with that in the word\n\\ - extra slash so that mysql treats it as a special char.\n##### Solution 2: regexp, \'^...|[:space:]...\' \uD83C\uDFAF\n##### Logic is the same as `like \'DIAB1%\' or conditions like \'% DIAB1%\'` but works faster \uD83D\uDD25\n##### \u2705 Runtime: 275 ms, faster than 93.47% of MySQL \uD83D\uDD25\n```\nselect *\nfrom patients\nwhere conditions regexp \'^DIAB1|[:space:]DIAB1\'\n```\n##### https://regexr.com is useful for building your regex and try it with different test case. \uD83E\uDD37\uD83C\uDFFB\u200D\u2640\uFE0F\n##### If you like the solutions, please upvote \uD83D\uDD3C\n##### For any questions, or discussions, comment below. \uD83D\uDC47\uFE0F
13
0
['MySQL']
3
patients-with-a-condition
🐘PostgreSQL ❗ Beats 94.35% ❗ Simple solution with REGEX
postgresql-beats-9435-simple-solution-wi-lzh6
Approach\nThis SQL query aims to select all columns and rows from a table named "patients" where the "conditions" column contains the string "DIAB1" at the begi
IliaAvdeev
NORMAL
2024-05-22T08:52:27.956567+00:00
2024-05-22T08:52:47.746556+00:00
1,381
false
# Approach\nThis SQL query aims to select all columns and rows from a table named "patients" where the "conditions" column contains the string "DIAB1" at the beginning of a word. Let\'s break it down:\n\n* **`SELECT *`**: This instructs the database to retrieve all columns (`*`) from the table.\n* **`FROM patients`**: Specifies that the data should be retrieved from the table named "patients".\n* **`WHERE conditions ~ \'\\mDIAB1\'`**: This is the filtering clause:\n * **`WHERE`**: Indicates that a condition will be applied to filter the results.\n * **`conditions`**: Refers to a column likely named "conditions" within the "patients" table.\n * **`~`**: This is a symbol used in some SQL dialects (like PostgreSQL) for regular expression matching. It checks if the pattern on the right side matches the data in the "conditions" column.\n * **`\'\\mDIAB1\'`**: This is the regular expression pattern. \n * **`\\m`**: This signifies the beginning of a word boundary. This ensures you\'re only matching "DIAB1" when it\'s a separate word or at the very beginning of the "conditions" value.\n * **`DIAB1`**: The specific string you\'re searching for within the "conditions" column.\n\n# Code\n```\nSELECT * FROM patients\nWHERE conditions ~ \'\\mDIAB1\'\n```
11
0
['PostgreSQL']
1
patients-with-a-condition
Pandas and MySQL with explanation
pandas-and-mysql-with-explanation-by-spe-bxyt
Pandas\npy []\nimport pandas as pd\n\ndef find_patients(patients: pd.DataFrame) -> pd.DataFrame:\n mask = patients[\'conditions\'].str.contains(r\'\\bDIAB1\'
speedyy
NORMAL
2023-09-22T09:15:46.182816+00:00
2023-09-23T17:22:36.035542+00:00
1,322
false
## Pandas\n```py []\nimport pandas as pd\n\ndef find_patients(patients: pd.DataFrame) -> pd.DataFrame:\n mask = patients[\'conditions\'].str.contains(r\'\\bDIAB1\')\n return patients[mask]\n```\nWithout r and adding regex=True doesn\'t work -- (\'\\bDIAB1\', regex=True)\nBing explained :\n------------------\nNo, the `r` prefix does not mean regex. It means raw string, which is a way of writing strings in Python that do not interpret escape sequences. The `r` prefix does not affect how str.contains() interprets the string, it only affects how Python interprets the string. A raw string is a way of telling Python to ignore the escape sequences and treat them as literal characters. This does not mean that str.contains() will also ignore them and treat them as literal characters. The str.contains() method will still read them as special characters and use them to match the pattern in the column.\n\nYou can think of it like this: Python reads the raw string and passes it to str.contains() without changing anything. Then, str.contains() reads the string and applies the rules of regular expressions to find the matches in the column.\n\n`\\b` is a backspace when it is used in a normal string, but it is a word boundary when it is used in a regular expression. The str.contains() method interprets the regular expression according to the rules of the Python re module where `\\b` means `word boundary` not backspace..\n\nIf you use `str.contains(\'\\bDIAB1\') as a normal string`, Python will interpret the `\\ b` as a special character that means a backspace. This means that it will erase the previous character in the string. For example, \'cat\\b\' will become \'ca\'. This is not what you want when you are looking for the condition \u2018DIAB1\u2019 as a FRONT PART OF A WORD. You want to use the `\\b` as a metacharacter that means `a word boundary`..\n\nSUMMARY : Let Python read `\'\\bDIAB1\'` as a raw string(`r\'\\bDIAB1\'`) then let str.contains( ) read it as regular expression i.e. regex.\n\n## A easy way, use \' \\ \' before \'\\b\' to work \'\\b\' as a boundary word because \n- `\\b` in a normal string means \'backspace\' but in regular expression it\'s a `boundary word` and we want `\\b` to work as a `boundary word` NOT AS A `backspace`, so use `\\` before `\\b` (`\\\\b`) to work it as a special character in regular expression. (This same theory works for MySQL as well)\n \n \'\\\\bDIAB1\'\n\n```py []\nimport pandas as pd\n\ndef find_patients(patients: pd.DataFrame) -> pd.DataFrame:\n mask = patients[\'conditions\'].str.contains(\'\\\\bDIAB1\')\n return patients[mask]\n```\n\n## MySQL\n```sql []\nSELECT * \nFROM patients\nWHERE conditions REGEXP \'\\\\bDIAB1\'\n```\n\\b won\'t work as a boundary word if you don\'t put a `\\` before `\\b` in MySQL.\n\n### If the post was helpful, an upvote will really be appreciated. Thank Youu
11
0
['MySQL', 'Pandas']
1
patients-with-a-condition
MySQL solution using LIKE
mysql-solution-using-like-by-victron777-m1t8
\nselect *\nfrom patients\nwhere conditions like \'DIAB1%\' or\n conditions like \'% DIAB1%\'\n
victron777
NORMAL
2021-02-12T01:36:30.909282+00:00
2021-02-12T01:36:30.909318+00:00
3,260
false
```\nselect *\nfrom patients\nwhere conditions like \'DIAB1%\' or\n conditions like \'% DIAB1%\'\n```
11
0
[]
2
patients-with-a-condition
MySQL Solution ONE_LINE, FAST, CLEAN, EASY
mysql-solution-one_line-fast-clean-easy-9z1zj
Please UPVOTE \uD83D\uDD25\n\n REGEXP solution FASTER than 82%, One line\n\n\nSELECT * FROM Patients WHERE conditions REGEXP \'\\\\bDIAB1\'\n\t\t\t// "\\b allow
YassineAmmani
NORMAL
2022-08-29T22:53:29.880589+00:00
2022-08-29T22:53:29.880648+00:00
1,139
false
***Please UPVOTE \uD83D\uDD25***\n\n* REGEXP solution FASTER than 82%, One line\n\n```\nSELECT * FROM Patients WHERE conditions REGEXP \'\\\\bDIAB1\'\n\t\t\t// "\\b allows you to perform a \u201Cwhole words only\u201D search using a regular expression in the form of \\bword\\b //\n```\n\n***Please UPVOTE \uD83D\uDD25***\n* LIKE solution Faster than 78%\n\n```\nSELECT * \nFROM Patients \nWHERE \n\tconditions like \'DIAB1%\' \n\tor conditions like \'% DIAB1%\'\n```\n\n***Please UPVOTE \uD83D\uDD25***\n\n
10
0
['MySQL']
0
patients-with-a-condition
Simple Solution MSSQL Server
simple-solution-mssql-server-by-danylozd-77lt
\n\n\nIn this exercies we need to find all patiants who have Type I Diabetes. \nType I Diabetes always starts with DIAB1 prefix.\n\nSolution:\n\n\nselect *\nfro
danylozdoryk1
NORMAL
2022-08-15T09:23:52.425881+00:00
2022-08-15T09:23:52.425923+00:00
1,360
false
\n\n\nIn this exercies we need to find all patiants who have Type I Diabetes. \nType I Diabetes always starts with **DIAB1** prefix.\n\n***Solution:***\n\n```\nselect *\nfrom Patients\nwhere conditions like \'% DIAB1%\'\nor conditions like \'DIAB1%\'\n```\n\nThe **easiest** way here is using ```LIKE ``` operator.\n\nIn first two rows we simply select all records from table **Patients**.\n\n```\nselect *\nfrom Patients\n```\n\nNow we need to filter our records.\nWe use ```LIKE``` operator.\nThe ```LIKE``` operator is used in a ```WHERE``` clause to search for a specified pattern in a column.\n\nThere are two wildcards often used in conjunction with the ```LIKE``` operator:\n\n The percent sign % represents zero, one, or multiple characters\n The underscore sign _ represents one, single character\n\nIN our case we use only % charachter\n\n```\nwhere conditions like \'% DIAB1%\'\nor conditions like \'DIAB1%\'\n```\n\nSo this one ```where conditions like \'% DIAB1%\'``` means that we are looking for all conditions(this is column name) that are satisfying 2 things:\n\n1.Before **"DIAB1"** will be *1 white space* and before whitespace will be *0 or more* charachters. (**% DIAB1** - first part).\n2.And after **"DIAB1"** will be *0 or more* charachters. (**DIAB1%** - second part). \n\nIn the next condition ```or conditions like \'DIAB1%\'``` I used the same logic. There are two conditions in this one:\n1. String always starts with **\'DIAB1\'**. (**DIAB1** - first part)\n2 .There can be 0 or more chars after **\'DIAB1\'**. (**DIAB1%** - second part)\n\n**Question:**\n1. Why we need both \n```\nwhere conditions like \'% DIAB1%\'\nor conditions like \'DIAB1%\'\n```\n\n**Answer**\nIn first one (```where conditions like \'% DIAB1%\'```): we choose "DIabetes" ike a **second, third, etc.** condition \n\nFor example: \nPatients table:\n+------------+--------------+--------------+\n| patient_id | patient_name | conditions |\n+------------+--------------+--------------+\n| 1 | Daniel | YFEV COUGH |\n**| 3 | Bob | DIAB100 MYOP |**\n**| 4 | George | ACNE DIAB100 |**\n\nThis one(```where conditions like \'% DIAB1%\'```) will choose **"George"** (*patientid: 4*), because he has "ACNE **DIAB100**" so Diabetes comes like a **second** condition here. But this clause **will not** choose "Bob" (*patientid: 3*) because in **Bobs** case Diabetes comes like a **first** condition.\n\nAnd **for this purposes** we need clause **or**:\n```or conditions like \'DIAB1%\'```\nThis clause **will choose Bob** but **not George** because it chooses every patient who has Diabetes like a **first** condition.\n\n\n__________________________________________________________________\n\nWrite me if you have any questioons :) .\n\n\n.\n
10
0
['MySQL', 'MS SQL Server']
2
patients-with-a-condition
Pandas - Simple solution
pandas-simple-solution-by-allen99-6k6j
Approach\n1. Retrieve the content of the conditions field.\n2. Create a function to split each conditions by a space \' \' and check whether it starts with DIAB
Allen99
NORMAL
2023-08-12T02:59:21.250645+00:00
2023-08-12T02:59:21.250693+00:00
964
false
# Approach\n1. Retrieve the content of the `conditions` field.\n2. Create a function to split each `conditions` by a space `\' \'` and check whether it starts with `DIAB1` or not.\n\n# Code\n```\nimport pandas as pd\n\ndef find_patients(patients: pd.DataFrame) -> pd.DataFrame:\n return patients[patients[\'conditions\'].apply(is_valid)]\n\ndef is_valid(content: str) -> bool:\n return any([x.startswith(\'DIAB1\') for x in content.split()])\n```
9
0
['Python3', 'Pandas']
0
patients-with-a-condition
Problem Solution
problem-solution-by-mostafasg-m1yq
Intuition\n Describe your first thoughts on how to solve this problem. \n\n# Approach\n Describe your approach to solving the problem. \n\n# Complexity\n- Time
MostafaSG
NORMAL
2024-07-03T18:33:35.843669+00:00
2024-07-03T18:33:35.843701+00:00
578
false
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n\n# Complexity\n- Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n\n# Code\n```\n-- Write your PostgreSQL query statement below\nselect patient_id , patient_name , conditions\nfrom Patients \n-- here we have two cases \n-- case 1 : condition start with \'DIAB1\' \n-- case 2 : condition does not start with \'DIABI\' but also refers to Type I Diabetes\nwhere conditions like \'% DIAB1%\' or conditions like \'DIAB1%\';\n```
8
0
['PostgreSQL']
0
patients-with-a-condition
Beats 100% | All Details mentioned | MySQL, SQL server, Oracle | Best Solution |
beats-100-all-details-mentioned-mysql-sq-i04c
Please Upvote !!\n\n\n - select *: Selects all columns from the specified table.\n - from Patients: Specifies the table to retrieve data from (Patients).\n - wh
Ikapoor123
NORMAL
2024-02-14T06:25:21.048967+00:00
2024-02-21T16:14:23.830685+00:00
1,438
false
# Please Upvote !!\n\n\n - select *: Selects all columns from the specified table.\n - from Patients: Specifies the table to retrieve data from (Patients).\n - where: Introduces the filtering condition.\n - conditions REGEXP: Applies the regular expression operator (REGEXP) to the conditions column.\n - \'\\bDIAB1\': Defines the regular expression pattern:\n - \\b: Matches a word boundary (ensuring "DIAB1" isn\'t part of a larger word).\n - DIAB1: Matches the literal string "DIAB1".\n\n# Code\n```\nselect * from Patients where conditions REGEXP \'\\\\bDIAB1\';\n```
8
1
['Database', 'MySQL', 'Oracle', 'MS SQL Server']
1
patients-with-a-condition
SQL ✅ | LIKE ✅| Easy to understand
sql-like-easy-to-understand-by-jasurbeka-wip0
Intuition\nWe have two different situations. condition can consist of one word or several words:\n1. If it is one word \'DIAB1%\' would be enough.\n2. If it con
jasurbekaktamov081
NORMAL
2023-06-15T15:35:45.855170+00:00
2023-06-15T15:35:45.855192+00:00
570
false
# Intuition\nWe have two different situations. condition can consist of one word or several words:\n1. If it is one word \'DIAB1%\' would be enough.\n2. If it consists of several words, we add \'% DIAB1%\' so that the beginning of an optional word starts with "DIAB1"\n\n![image.png](https://assets.leetcode.com/users/images/df766da9-266b-40e9-b18f-1602b194cb05_1686843337.7371843.png)\n\n\n# Code\n```\nselect *from Patients \nwhere conditions like \'DIAB1%\' or conditions like \'% DIAB1%\'\n```
8
0
['Database', 'MySQL']
2
patients-with-a-condition
1527. Patients With a Condition
1527-patients-with-a-condition-by-spauld-vxzy
```\nSELECT * FROM Patients\nWHERE conditions LIKE "DIAB1%" \nOR conditions LIKE "% DIAB1%" ;
Spaulding_
NORMAL
2022-09-08T08:34:48.726142+00:00
2022-09-08T08:34:48.726181+00:00
581
false
```\nSELECT * FROM Patients\nWHERE conditions LIKE "DIAB1%" \nOR conditions LIKE "% DIAB1%" ;
8
0
['MySQL']
0
patients-with-a-condition
[MySQL] BEATS 100.00% MEMORY/SPEED 0ms // APRIL 2022
mysql-beats-10000-memoryspeed-0ms-april-jy2ys
\nSELECT * FROM PATIENTS WHERE\nCONDITIONS LIKE \'% DIAB1%\' OR\nCONDITIONS LIKE \'DIAB1%\';\n
darian-catalin-cucer
NORMAL
2022-04-23T09:28:25.205757+00:00
2022-04-23T09:28:25.205785+00:00
1,457
false
```\nSELECT * FROM PATIENTS WHERE\nCONDITIONS LIKE \'% DIAB1%\' OR\nCONDITIONS LIKE \'DIAB1%\';\n```
8
0
['MySQL']
2
patients-with-a-condition
Easy to Understand | SQL
easy-to-understand-sql-by-sai721-1yop
\nselect\n patient_id,\n patient_name,\n conditions\nfrom\n patients\nwhere\n conditions like \'% DIAB1%\' #any condition in middle like ACNE DI
sai721
NORMAL
2022-04-19T10:30:45.358033+00:00
2022-04-19T10:30:45.358075+00:00
943
false
```\nselect\n patient_id,\n patient_name,\n conditions\nfrom\n patients\nwhere\n conditions like \'% DIAB1%\' #any condition in middle like ACNE DIAB100\n or\n conditions like \'DIAB1%\'; #starts with DIAB1\n```\n\nIf you have any **doubts**, feel **free to ask**...\nIf you understand the **concept**. Don\'t Forget to **upvote**\n\n
8
0
['MySQL']
2
patients-with-a-condition
✅EASY MYSQL SOLUTION
easy-mysql-solution-by-swayam28-lv4l
Intuition\n Describe your first thoughts on how to solve this problem. \n\n# Approach\n Describe your approach to solving the problem. \n\n# Complexity\n- Time
swayam28
NORMAL
2024-08-23T08:48:59.455976+00:00
2024-08-23T08:48:59.455998+00:00
2,893
false
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n\n# Complexity\n- Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n\n# Code\n![Up.png](https://assets.leetcode.com/users/images/bcb0b3fb-d33f-46b0-9c58-784c993c4f6a_1724402937.59048.png)\n\n```mysql []\nSELECT * FROM patients WHERE conditions REGEXP \'\\\\bDIAB1\'\n```
7
0
['MySQL']
4
patients-with-a-condition
Easy SQL Query
easy-sql-query-by-deepakumar-developer-5s0k
\n\n# Query\n\nSELECT patient_id, patient_name, conditions\nFROM Patients\nWHERE conditions LIKE \'DIAB1%\' OR conditions LIKE \'% DIAB1%\' \nORDER BY patient_i
deepakumar-developer
NORMAL
2023-12-28T03:08:43.567207+00:00
2023-12-28T03:08:43.567224+00:00
1,291
false
\n\n# Query\n```\nSELECT patient_id, patient_name, conditions\nFROM Patients\nWHERE conditions LIKE \'DIAB1%\' OR conditions LIKE \'% DIAB1%\' \nORDER BY patient_id;\n\n```
7
0
['MySQL']
3
patients-with-a-condition
PANDAS 1 LINER
pandas-1-liner-by-manjarinm10-ywjx
\n# Code\n\nimport pandas as pd\n\ndef find_patients(patients: pd.DataFrame) -> pd.DataFrame:\n return patients[patients[\'conditions\'].str.contains(\' DIAB
manjarinm10
NORMAL
2023-08-10T14:33:32.866816+00:00
2023-08-10T14:33:32.866850+00:00
632
false
\n# Code\n```\nimport pandas as pd\n\ndef find_patients(patients: pd.DataFrame) -> pd.DataFrame:\n return patients[patients[\'conditions\'].str.contains(\' DIAB1\') | patients[\'conditions\'].str.startswith(\'DIAB1\')]\n```
7
0
['Pandas']
0
patients-with-a-condition
MySQL | 2 Solutions using LIKE and RLIKE (REGEX matching)
mysql-2-solutions-using-like-and-rlike-r-7jzs
Pattern matching using LIKE:\nSQL\nSELECT *\nFROM Patients\nWHERE conditions LIKE \'DIAB1%\' OR conditions LIKE \'% DIAB1%\'\n\n- We can add a white space to th
ahmadheshamzaki
NORMAL
2022-07-12T11:47:43.428599+00:00
2022-07-13T11:51:08.860408+00:00
718
false
1. Pattern matching using `LIKE`:\n```SQL\nSELECT *\nFROM Patients\nWHERE conditions LIKE \'DIAB1%\' OR conditions LIKE \'% DIAB1%\'\n```\n- We can add a white space to the start of `conditions` to avoid searching for 2 different patterns:\n```SQL\nSELECT *\nFROM Patients\nWHERE CONCAT(\' \', conditions) LIKE \'% DIAB1%\'\n```\n\n2. REGEX pattern matching using `RLIKE`:\n```SQL\nSELECT *\nFROM Patients\nWHERE conditions RLIKE \'\\\\bDIAB1\'\n```\n- `\\b` in regex refers to word boundaries. So `\\\\bDIAB1` matches any string with a word that starts with `DIAB1`.
7
0
['MySQL']
0
patients-with-a-condition
Oracle CORRECT answer
oracle-correct-answer-by-krystali-fq33
Since all oracle solution I saw doesn\'t work...\nill post mine\n\nselect *\nfrom Patients\nwhere regexp_like(conditions, \'(\\s+|^)DIAB1\')\n
krystali
NORMAL
2021-01-02T20:53:40.684580+00:00
2021-01-02T20:53:40.684624+00:00
642
false
Since all oracle solution I saw doesn\'t work...\nill post mine\n```\nselect *\nfrom Patients\nwhere regexp_like(conditions, \'(\\s+|^)DIAB1\')\n```
7
0
[]
2
patients-with-a-condition
MySQL: 2 solutions
mysql-2-solutions-by-bunxi-5hzg
Using simple string matching:\n\nSELECT * FROM Patients\nWHERE conditions LIKE \'%DIAB1%\';\n\n\nUsing regular expression match:\n\nSELECT * FROM Patients\nWHER
bunxi
NORMAL
2020-07-26T00:54:14.346374+00:00
2020-07-26T13:55:32.631824+00:00
2,536
false
Using simple string matching:\n```\nSELECT * FROM Patients\nWHERE conditions LIKE \'%DIAB1%\';\n```\n\nUsing regular expression match:\n```\nSELECT * FROM Patients\nWHERE conditions REGEXP \'^DIAB1| DIAB1\';\n```\n\nThe REGEXP version is more robust.
7
4
[]
3
patients-with-a-condition
USING LOCATE() FUNCTION | EFFICIENT
using-locate-function-efficient-by-ashir-v929
Intuition\nThe goal is to retrieve records from the Patients table where the condition \'DIAB1\' is either the first entry in the conditions string or is preced
ashirvad_47
NORMAL
2024-09-23T16:13:07.857568+00:00
2024-09-23T16:13:07.857597+00:00
1,156
false
# Intuition\nThe goal is to retrieve records from the `Patients` table where the condition \'DIAB1\' is either the first entry in the `conditions` string or is preceded by a space. This indicates that \'DIAB1\' is either the only condition listed or part of a space-separated list of conditions. My initial thought was to use the `LOCATE` function to identify the presence and position of \'DIAB1\' within the string.\n\n# Approach\nTo solve this problem, we will use the `LOCATE` function in MySQL, which returns the position of a substring within a string. The approach involves the following steps:\n\n1. Check if \'DIAB1\' is at the beginning of the `conditions` string by using `LOCATE(\'DIAB1\', conditions) = 1`.\n2. Check if \'DIAB1\' is found elsewhere in the string, ensuring it is preceded by a space by using `LOCATE(\' DIAB1\', conditions) != 0`.\n3. Combine both conditions using the `OR` operator to retrieve the relevant records.\n\n# Complexity\n- **Time complexity**:\n - The time complexity of this query is approximately **O(n)**, where `n` is the number of rows in the `Patients` table. This is because each row\'s `conditions` string must be evaluated to determine the presence and position of \'DIAB1\'.\n\n- **Space complexity**:\n - The space complexity is **O(1)**, as we are not utilizing any additional data structures that grow with the input size; we are only returning results based on the existing data.\n\n# Code\n```mysql\nSELECT *\nFROM Patients\nWHERE LOCATE(\'DIAB1\', conditions) = 1\n OR LOCATE(\' DIAB1\', conditions) != 0;\n
6
0
['MySQL']
4
patients-with-a-condition
Simple Code || MySQL
simple-code-mysql-by-me_avi-eol4
Intuition\n Describe your first thoughts on how to solve this problem. \n\n# Approach\n Describe your approach to solving the problem. \n\n# Complexity\n- Time
me_avi
NORMAL
2023-10-11T16:20:34.180816+00:00
2023-10-11T16:20:34.180838+00:00
1,899
false
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n\n# Complexity\n- Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n\n# Code\n```\n# Write your MySQL query statement below\nselect patient_id, patient_name, conditions\nfrom patients\nwhere conditions regexp \'\\\\bDIAB1\'\n```
6
0
['MySQL']
1
patients-with-a-condition
Pandas Easiest Solution
pandas-easiest-solution-by-shreyaahadkar-c071
\n\n# Approach\nThe regular expression pattern r\'\bDIAB1\' means:\n\n\b: Match a word boundary.\nDIAB1: Match the string "DIAB1".\n\nThis pattern will match ro
shreyaahadkar
NORMAL
2023-08-08T06:16:26.614191+00:00
2023-08-08T06:16:26.614214+00:00
718
false
\n\n# Approach\nThe regular expression pattern **r\'\\bDIAB1\'** means:\n\n\\b: Match a word boundary.\nDIAB1: Match the string "DIAB1".\n\nThis pattern will match rows where the \'conditions\' column contains the word "DIAB1" as a whole word.\n\n# Code\n```\nimport pandas as pd\n\ndef find_patients(patients: pd.DataFrame) -> pd.DataFrame:\n result = patients[patients[\'conditions\'].str.contains(r\'\\bDIAB1\')]\n return result\n```
6
0
['Pandas']
0
patients-with-a-condition
Simple Solution using LIKE
simple-solution-using-like-by-profession-ehya
\n# Code\n\nSELECT * FROM PATIENTS WHERE\nCONDITIONS LIKE \'% DIAB1%\' OR\nCONDITIONS LIKE \'DIAB1%\';\n
ProfessionalMonk
NORMAL
2023-08-01T20:10:00.476321+00:00
2023-08-01T20:10:00.476346+00:00
1,342
false
\n# Code\n```\nSELECT * FROM PATIENTS WHERE\nCONDITIONS LIKE \'% DIAB1%\' OR\nCONDITIONS LIKE \'DIAB1%\';\n```
6
0
['MySQL']
0
patients-with-a-condition
Patients with a Condition
patients-with-a-condition-by-esdark-d664
Intuition\nPlease Upvote\n\n# Complexity\n- Time complexity:\n - 306ms\n- 90.19% Beatse\n\n\n\n# Code\n\n# Write your MySQL query statement below\nSELECT * F
esdark
NORMAL
2022-11-24T05:14:48.021856+00:00
2022-11-24T05:14:48.021898+00:00
1,127
false
# Intuition\nPlease Upvote\n\n# Complexity\n- Time complexity:\n - 306ms\n- 90.19% Beatse\n\n\n\n# Code\n```\n# Write your MySQL query statement below\nSELECT * FROM Patients\nWHERE conditions LIKE \'% DIAB1%\'\nOR conditions LIKE \'DIAB1%\'\n```
6
0
['MySQL']
0
patients-with-a-condition
SQL SERVER Solution
sql-server-solution-by-sharath_bayyaram-v1fw
Since we can\'t use all regular expression in sql server. We need to write separate conditions\n\nUpvote! \n\n# Code\n\n/* Write your T-SQL query statement belo
Sharath_Bayyaram
NORMAL
2022-10-24T10:51:47.988161+00:00
2022-10-24T16:47:58.900215+00:00
2,291
false
Since we can\'t use all regular expression in sql server. We need to write separate conditions\n\n**Upvote!** \n\n# Code\n```\n/* Write your T-SQL query statement below */\nselect patient_id, patient_name, conditions from Patients where conditions like \'DIAB1%\' or conditions like \'% DIAB1%\'\n```
6
0
['MS SQL Server']
1
patients-with-a-condition
✅✅ Easy Patern Searching with LIKE Patients With a Condition
easy-patern-searching-with-like-patients-33uq
\nSELECT Patients.patient_id, Patients.patient_name, Patients.conditions \nFROM Patients\nWHERE Patients.conditions LIKE \'DIAB1%\' OR \nPatients.conditions LIK
Arpit507
NORMAL
2022-09-24T11:32:06.830552+00:00
2022-09-24T11:32:06.830594+00:00
1,855
false
```\nSELECT Patients.patient_id, Patients.patient_name, Patients.conditions \nFROM Patients\nWHERE Patients.conditions LIKE \'DIAB1%\' OR \nPatients.conditions LIKE \'% DIAB1%\'\n```
6
0
['MySQL']
1
patients-with-a-condition
✅MySQL || Beginner level ||1Liner Solution||Simple-Short -Solution✅
mysql-beginner-level-1liner-solutionsimp-kkt0
Please upvote to motivate me in my quest of documenting all leetcode solutions. HAPPY CODING:)\nAny suggestions and improvements are always welcome.\n==========
Anos
NORMAL
2022-09-02T19:01:09.078165+00:00
2022-09-02T19:01:09.078191+00:00
835
false
**Please upvote to motivate me in my quest of documenting all leetcode solutions. HAPPY CODING:)\nAny suggestions and improvements are always welcome.***\n*====================================================================*\n\u2705 **MySQL Code :**\n```\nSELECT * FROM PATIENTS WHERE CONDITIONS LIKE \'% DIAB1%\' OR CONDITIONS LIKE \'DIAB1%\';\n```\n**Runtime:** 278 ms\n**Memory Usage:** 0B\n________________________________\n__________________________________\n\nIf you like the solution, please upvote \uD83D\uDD3C\nFor any questions, or discussions, comment below. \uD83D\uDC47\uFE0F
6
0
['MySQL']
1
patients-with-a-condition
Regexp works as it appears the problem has changed
regexp-works-as-it-appears-the-problem-h-0kml
This solultion uses regexp_like to test for the condition containing \'DIAB1\' preceeded by 1 to many spaces or for the condition starting with \'DIAB1\'\n\n\ns
blueghost79
NORMAL
2020-12-18T01:07:33.869260+00:00
2020-12-18T01:07:33.869294+00:00
1,427
false
This solultion uses regexp_like to test for the condition containing \'DIAB1\' preceeded by 1 to many spaces or for the condition starting with \'DIAB1\'\n\n```\nselect patient_id,\n patient_name,\n conditions\nfrom patients\nwhere regexp_like(conditions, \' +DIAB1|^DIAB1\')\n```
6
0
[]
5
patients-with-a-condition
Doctor's Orders: Catching DIAB1 Patients! 😷💉
doctors-orders-catching-diab1-patients-b-blwt
IntuitionWe need to filter patients whose conditions contain at least one word that starts with "DIAB1". Since "conditions" is a space-separated string of diffe
NoobML
NORMAL
2025-03-01T12:11:00.742772+00:00
2025-03-01T12:11:00.742772+00:00
911
false
# **Intuition** We need to filter patients whose **conditions** contain at least one word that **starts with** `"DIAB1"`. Since `"conditions"` is a space-separated string of different illnesses, we can't directly check for `"DIAB1"` as a substring. Instead, we must ensure that `"DIAB1"` appears as a **separate word**, not as part of another condition like `"DIAB201"`. For example: ✅ `"DIAB100 MYOP"` → **Valid** (contains `"DIAB100"`) ✅ `"ACNE DIAB100"` → **Valid** (contains `"DIAB100"`) ❌ `"DIAB201"` → **Invalid** (`DIAB2` does not match `DIAB1`) ❌ `"ACNE +DIAB100"` → **Invalid** (`+DIAB100` is not a separate word) # **Approach** 1. **Use Regular Expressions (`regex`) to find words that start with `"DIAB1"`** - We need to ensure that `"DIAB1"` is a standalone word. A simple `.str.contains('DIAB1')` would also match `"DIAB1000"` or `"DIAB201"`, which is incorrect. - To fix this, we use `(^|\s)DIAB1\d*($|\s)`, which ensures: - `"DIAB1"` appears **at the start (`^`) or after a space (`\s`)** - It is **followed by a number (`\d*`), a space (`\s`), or the end of the string (`$`)** - This prevents false matches with `"DIAB201"` or `"SOMEDIAB100"`. 2. **Apply the regex filter using `.str.contains()`** - `patients['conditions'].str.contains(r'(^|\s)DIAB1\d*($|\s)', na=False)` - This checks if any row in `"conditions"` contains a valid `"DIAB1"` condition. - `na=False` ensures that missing values (`NaN`) are treated as `False`, avoiding errors. 3. **Return the filtered DataFrame** - The rows where `"conditions"` match our regex are selected and returned. # **Complexity** - **Time Complexity:** **O(n)** - We iterate through **n rows** and apply a regex match for each row. - Regex `.str.contains()` runs in **O(m)** time per row (where `m` is the string length), but since medical conditions are short, it approximates to **O(1)** per row. - Overall, it scales linearly with **O(n)**. - **Space Complexity:** **O(1)** - We do **not** use extra data structures. - We filter the DataFrame **in place**, so memory usage is constant. 🚀 Upvote if this helped! It keeps me motivated to post more solutions! ![images.png](https://assets.leetcode.com/users/images/f0ab5afd-1554-4700-ae42-0373d36dd0b2_1740830928.9827623.png) ```python import pandas as pd def find_patients(patients: pd.DataFrame) -> pd.DataFrame: return patients[patients['conditions'].str.contains(r'(^|\s)DIAB1\d*($|\s)', na=False)] ```
5
0
['Pandas']
0
patients-with-a-condition
Simple Like operator Solution
simple-like-operator-solution-by-aadithy-u5wc
CodeThank you, aadithya18
aadithya18
NORMAL
2025-02-03T23:20:22.771788+00:00
2025-02-03T23:20:22.771788+00:00
1,745
false
# Code ```mysql [] # Write your MySQL query statement below SELECT * FROM Patients WHERE conditions LIKE '% DIAB1%' OR conditions LIKE 'DIAB1%'; ``` Thank you, aadithya18
5
0
['MySQL']
0
patients-with-a-condition
SQL || LIKE Operator || Easy Solution
sql-like-operator-easy-solution-by-shubh-tt8e
Intuition\n Describe your first thoughts on how to solve this problem. \n\n# Approach\n Describe your approach to solving the problem. \n\n# Complexity\n- Time
Shubhamjain287
NORMAL
2023-03-01T05:27:45.678453+00:00
2023-03-01T05:27:45.678495+00:00
1,431
false
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n\n# Complexity\n- Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n\n# Code\n```\n# Write your MySQL query statement below\nSELECT * from Patients\nWHERE conditions LIKE \'DIAB1%\'\nOR conditions LIKE \'% DIAB1%\';\n```
5
0
['MySQL']
0
patients-with-a-condition
Oracle | Or | Union
oracle-or-union-by-gowthamguttula-kup4
Please upvote, if it helps\n\n\n/* Write your PL/SQL query statement below */\n\n/*Using or */\nSelect * from Patients where conditions like \'% DIAB1%\' or con
GowthamGuttula
NORMAL
2022-04-12T06:32:17.427261+00:00
2022-04-12T06:34:45.706674+00:00
547
false
Please upvote, if it helps\n\n```\n/* Write your PL/SQL query statement below */\n\n/*Using or */\nSelect * from Patients where conditions like \'% DIAB1%\' or conditions like \'DIAB1%\'\n\n/*Using union */\nSelect * from Patients where conditions like \'% DIAB1%\'\nunion\nSelect * from Patients where conditions like \'DIAB1%\'\n\n```
5
0
['Union Find', 'Oracle']
0
patients-with-a-condition
Using Wild Cards
using-wild-cards-by-eshwaraprasad-vm0a
\n\n# Code\nmysql []\nSELECT * \nFROM Patients\nWHERE \n conditions LIKE "DIAB1%"\nOR \n conditions LIKE "% DIAB1%";\n
eshwaraprasad
NORMAL
2024-11-17T17:30:34.765909+00:00
2024-11-17T17:30:34.765934+00:00
1,067
false
\n\n# Code\n```mysql []\nSELECT * \nFROM Patients\nWHERE \n conditions LIKE "DIAB1%"\nOR \n conditions LIKE "% DIAB1%";\n```
4
0
['MySQL']
0
patients-with-a-condition
easy solution
easy-solution-by-ngocnhi22-n1df
Intuition\n Describe your first thoughts on how to solve this problem. \n\n# Approach\n Describe your approach to solving the problem. \n\n# Complexity\n- Time
Ngocnhi22
NORMAL
2024-04-23T08:00:26.514664+00:00
2024-04-23T08:00:26.514694+00:00
1,029
false
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n\n# Complexity\n- Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n\n# Code\n```\n# Write your MySQL query statement below\nselect patient_id, patient_name, conditions\nfrom patients\nwhere conditions like \'%_% DIAB1%\'\n or conditions like \'DIAB1%\'\n```
4
0
['MySQL']
1
patients-with-a-condition
Oracle Easy Regex Solution
oracle-easy-regex-solution-by-user7496po-scq1
\nSELECT * FROM patients\nWHERE REGEXP_LIKE(conditions, \'(^| )DIAB1\')\n\nThis is the same as:\n\nSELECT * FROM patients\nWHERE conditions LIKE \'% DIAB1%\' OR
user7496Po
NORMAL
2023-11-26T23:11:42.606157+00:00
2023-11-26T23:13:20.080337+00:00
504
false
```\nSELECT * FROM patients\nWHERE REGEXP_LIKE(conditions, \'(^| )DIAB1\')\n```\nThis is the same as:\n```\nSELECT * FROM patients\nWHERE conditions LIKE \'% DIAB1%\' OR conditions LIKE \'DIAB1%\';\n```\n(^| ): This part of the pattern uses the | (alternation) operator within parentheses to match either the start of the string ^ or a space character
4
0
['Oracle']
0
patients-with-a-condition
Find Patients with a Condition using MySQL Wildcard
find-patients-with-a-condition-using-mys-imtl
Intuition\n Describe your first thoughts on how to solve this problem. \nWe need to find patients with Type I Diabetes.\n\n- Type I Diabetes always starts with
samabdullaev
NORMAL
2023-10-25T14:57:25.352081+00:00
2023-10-25T14:57:25.352105+00:00
700
false
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nWe need to find patients with Type I Diabetes.\n\n- Type I Diabetes always starts with `DIAB1` prefix.\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n1. Select specific columns from the table.\n\n > `SELECT` \u2192 This command retrieves data from a database\n\n > Asterisk (`*`) \u2192 This symbol specifies that the query should return all columns of the queried tables\n\n2. Filter the results to include only those patients with `DIAB1` prefix.\n\n > `WHERE` \u2192 This command filters a result set to include only records that fulfill a specified condition\n\n > `OR` \u2192 This operator displays a record if any of the conditions separated by it are `TRUE`\n\n > `LIKE` \u2192 This operator searches for a specified pattern in a column\n\n > `%` \u2192 This symbol (wildcard) matches any sequence of characters\n\n > `wildcard characters` \u2192 These characters are used to substitute one or more characters in a string\n\n# Complexity\n- Time complexity: $O(n)$\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n`n` is the number of rows in the table. This is because the query processes each row in the table to check the conditions.\n\n# Code\n```\n# Write your MySQL query statement below\nSELECT *\nFROM Patients\nWHERE conditions LIKE \'% DIAB1%\' OR \n conditions LIKE \'DIAB1%\'\n```
4
0
['MySQL']
0
patients-with-a-condition
Pandas | SQL |Easy | Patients With a Condition
pandas-sql-easy-patients-with-a-conditio-t98o
see the Successfully Accepted Submission\n\n\nimport pandas as pd\n\ndef find_patients(patients: pd.DataFrame) -> pd.DataFrame:\n \n # First, we select `c
Khosiyat
NORMAL
2023-09-20T18:45:27.675873+00:00
2023-10-01T17:41:31.227766+00:00
178
false
[see the Successfully Accepted Submission](https://leetcode.com/submissions/detail/1054376226/)\n\n```\nimport pandas as pd\n\ndef find_patients(patients: pd.DataFrame) -> pd.DataFrame:\n \n # First, we select `conditions` column from the patients DataFrame\n patients_conditions = patients[\'conditions\']\n \n # THen, we access the string methods associated with the \'mail\' column by using `.str`\n str_patients= patients_conditions.str\n \n # By using the str.contains() we get a Boolean Series where each element is True if the corresponding text element contains the pattern "DIAB1" as a whole word and False otherwise.\n patients_with_diabeties = str_patients.contains(r\'\\bDIAB1\', regex=True)\n \n # Finally, we get a new DataFrame that includes only the rows `patients` where the corresponding value in patients_with_diabeties is True\n filtered_patients = patients[patients_with_diabeties]\n \n return filtered_patients\n```\n\n**SQL**\n\n[see the Successfully Accepted Submission](https://leetcode.com/submissions/detail/1061718663/)\n\n```\nSELECT * \nFROM Patients \nWHERE conditions \nLIKE \'DIAB1%\' \nOR \nconditions LIKE \'% DIAB1%\';\n```\n\n```\n-- Select all columns (asterisk *) from the \'Patients\' table\nSELECT * \n\n-- Retrieve data from the \'Patients\' table\nFROM Patients \n\n-- Apply a filter to select rows where the \'conditions\' column matches one of two patterns:\n-- 1. The \'conditions\' column starts with \'DIAB1\' (e.g., \'DIAB1 Type\')\n-- 2. The \'conditions\' column contains \' DIAB1\' anywhere in its content (e.g., \'Type DIAB1\')\nWHERE conditions LIKE \'DIAB1%\' \nOR \nconditions LIKE \'% DIAB1%\';\n```\n\n![image](https://assets.leetcode.com/users/images/682da435-5230-4b74-81b0-0cf270ef929c_1695235450.6390727.jpeg)\n\n\n# Pandas & SQL | SOLVED & EXPLAINED LIST\n\n**EASY**\n\n- [Combine Two Tables](https://leetcode.com/problems/combine-two-tables/solutions/4051076/pandas-sql-easy-combine-two-tables/)\n\n- [Employees Earning More Than Their Managers](https://leetcode.com/problems/employees-earning-more-than-their-managers/solutions/4051991/pandas-sql-easy-employees-earning-more-than-their-managers/)\n\n- [Duplicate Emails](https://leetcode.com/problems/duplicate-emails/solutions/4055225/pandas-easy-duplicate-emails/)\n\n- [Customers Who Never Order](https://leetcode.com/problems/customers-who-never-order/solutions/4055429/pandas-sql-easy-customers-who-never-order-easy-explained/)\n\n- [Delete Duplicate Emails](https://leetcode.com/problems/delete-duplicate-emails/solutions/4055572/pandas-sql-easy-delete-duplicate-emails-easy/)\n- [Rising Temperature](https://leetcode.com/problems/rising-temperature/solutions/4056328/pandas-sql-easy-rising-temperature-easy/)\n\n- [ Game Play Analysis I](https://leetcode.com/problems/game-play-analysis-i/solutions/4056422/pandas-sql-easy-game-play-analysis-i-easy/)\n\n- [Find Customer Referee](https://leetcode.com/problems/find-customer-referee/solutions/4056516/pandas-sql-easy-find-customer-referee-easy/)\n\n- [Classes More Than 5 Students](https://leetcode.com/problems/classes-more-than-5-students/solutions/4058381/pandas-sql-easy-classes-more-than-5-students-easy/)\n- [Employee Bonus](https://leetcode.com/problems/employee-bonus/solutions/4058430/pandas-sql-easy-employee-bonus/)\n\n- [Sales Person](https://leetcode.com/problems/sales-person/solutions/4058824/pandas-sql-easy-sales-person/)\n\n- [Biggest Single Number](https://leetcode.com/problems/biggest-single-number/solutions/4063950/pandas-sql-easy-biggest-single-number/)\n\n- [Not Boring Movies](https://leetcode.com/problems/not-boring-movies/solutions/4065350/pandas-sql-easy-not-boring-movies/)\n- [Swap Salary](https://leetcode.com/problems/swap-salary/solutions/4065423/pandas-sql-easy-swap-salary/)\n\n- [Actors & Directors Cooperated min Three Times](https://leetcode.com/problems/actors-and-directors-who-cooperated-at-least-three-times/solutions/4065511/pandas-sql-easy-explained-step-by-step-actors-directors-cooperated-min-three-times/)\n\n- [Product Sales Analysis I](https://leetcode.com/problems/product-sales-analysis-i/solutions/4065545/pandas-sql-easy-product-sales-analysis-i/)\n\n- [Project Employees I](https://leetcode.com/problems/project-employees-i/solutions/4065635/pandas-sql-easy-project-employees-i/)\n- [Sales Analysis III](https://leetcode.com/problems/sales-analysis-iii/solutions/4065755/sales-analysis-iii-pandas-easy/)\n\n- [Reformat Department Table](https://leetcode.com/problems/reformat-department-table/solutions/4066153/pandas-sql-easy-explained-step-by-step-reformat-department-table/)\n\n- [Top Travellers](https://leetcode.com/problems/top-travellers/solutions/4066252/top-travellers-pandas-easy-eaxplained-step-by-step/)\n\n- [Replace Employee ID With The Unique Identifier](https://leetcode.com/problems/replace-employee-id-with-the-unique-identifier/solutions/4066321/pandas-sql-easy-replace-employee-id-with-the-unique-identifier/)\n- [Group Sold Products By The Date](https://leetcode.com/problems/group-sold-products-by-the-date/solutions/4066344/pandas-sql-easy-explained-step-by-step-group-sold-products-by-the-date/)\n\n- [Customer Placing the Largest Number of Orders](https://leetcode.com/problems/customer-placing-the-largest-number-of-orders/solutions/4068822/pandas-sql-easy-explained-step-by-step-customer-placing-the-largest-number-of-orders/)\n\n- [Article Views I](https://leetcode.com/problems/article-views-i/solutions/4069777/pandas-sql-easy-article-views-i/)\n\n- [User Activity for the Past 30 Days I](https://leetcode.com/problems/user-activity-for-the-past-30-days-i/solutions/4069797/pandas-sql-easy-user-activity-for-the-past-30-days-i/)\n\n- [Find Users With Valid E-Mails](https://leetcode.com/problems/find-users-with-valid-e-mails/solutions/4069810/pandas-sql-easy-find-users-with-valid-e-mails/)\n\n- [Patients With a Condition](https://leetcode.com/problems/patients-with-a-condition/solutions/4069817/pandas-sql-easy-patients-with-a-condition/)\n\n- [Customer Who Visited but Did Not Make Any Transactions](https://leetcode.com/problems/customer-who-visited-but-did-not-make-any-transactions/solutions/4072542/pandas-sql-easy-customer-who-visited-but-did-not-make-any-transactions/)\n\n\n- [Bank Account Summary II](https://leetcode.com/problems/bank-account-summary-ii/solutions/4072569/pandas-sql-easy-bank-account-summary-ii/)\n\n- [Invalid Tweets](https://leetcode.com/problems/invalid-tweets/solutions/4072599/pandas-sql-easy-invalid-tweets/)\n\n- [Daily Leads and Partners](https://leetcode.com/problems/daily-leads-and-partners/solutions/4072981/pandas-sql-easy-daily-leads-and-partners/)\n\n- [Recyclable and Low Fat Products](https://leetcode.com/problems/recyclable-and-low-fat-products/solutions/4073003/pandas-sql-easy-recyclable-and-low-fat-products/)\n\n- [Rearrange Products Table](https://leetcode.com/problems/rearrange-products-table/solutions/4073028/pandas-sql-easy-rearrange-products-table/)\n- [Calculate Special Bonus](https://leetcode.com/problems/calculate-special-bonus/solutions/4073595/pandas-sql-easy-calculate-special-bonus/)\n\n- [Count Unique Subjects](https://leetcode.com/problems/number-of-unique-subjects-taught-by-each-teacher/solutions/4073666/pandas-sql-easy-count-unique-subjects/)\n\n- [Count Unique Subjects](https://leetcode.com/problems/number-of-unique-subjects-taught-by-each-teacher/solutions/4073666/pandas-sql-easy-count-unique-subjects/)\n\n- [Fix Names in a Table](https://leetcode.com/problems/fix-names-in-a-table/solutions/4073695/pandas-sql-easy-fix-names-in-a-table/)\n- [Primary Department for Each Employee](https://leetcode.com/problems/primary-department-for-each-employee/solutions/4076183/pandas-sql-easy-primary-department-for-each-employee/)\n\n- [The Latest Login in 2020](https://leetcode.com/problems/the-latest-login-in-2020/solutions/4076240/pandas-sql-easy-the-latest-login-in-2020/)\n\n- [Find Total Time Spent by Each Employee](https://leetcode.com/problems/find-total-time-spent-by-each-employee/solutions/4076313/pandas-sql-easy-find-total-time-spent-by-each-employee/)\n\n- [Find Followers Count](https://leetcode.com/problems/find-followers-count/solutions/4076342/pandas-sql-easy-find-followers-count/)\n- [Percentage of Users Attended a Contest](https://leetcode.com/problems/percentage-of-users-attended-a-contest/solutions/4077301/pandas-sql-easy-percentage-of-users-attended-a-contest/)\n\n- [Employees With Missing Information](https://leetcode.com/problems/employees-with-missing-information/solutions/4077308/pandas-sql-easy-employees-with-missing-information/)\n\n- [Average Time of Process per Machine](https://leetcode.com/problems/average-time-of-process-per-machine/solutions/4077402/pandas-sql-easy-average-time-of-process-per-machine/)\n
4
0
['Python', 'MySQL']
0
patients-with-a-condition
✅ Simple Pandas - String Method .contains()
simple-pandas-string-method-contains-by-6r4ce
Intuition\n Describe your first thoughts on how to solve this problem. \nSimply filtering by using the string method .contains() and pass in the regex pattern.\
Penrose3angle
NORMAL
2023-08-13T01:15:03.240260+00:00
2023-08-13T01:15:03.240279+00:00
1,003
false
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nSimply filtering by using the string method .contains() and pass in the regex pattern.\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n\n# Complexity\n- Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n\n# Code\n```\nimport pandas as pd\n\ndef find_patients(patients: pd.DataFrame) -> pd.DataFrame:\n # Regex pattern \n # The \\b in the pattern is a word boundary assertion that ensures \'DIAB1\' is a separate word and not part of another word\n # wildcard both in front and after according to the example\n filtered = patients[patients[\'conditions\'].str.contains(r\'\\bDIAB1\')]\n return filtered\n```
4
0
['Pandas']
1
patients-with-a-condition
MySQL Eassy Solution
mysql-eassy-solution-by-triyambkeshtiwar-fll8
Intuition\n Describe your first thoughts on how to solve this problem. \n\n# Approach\n Describe your approach to solving the problem. \n\n# Complexity\n- Time
triyambkeshtiwari
NORMAL
2023-08-05T14:16:08.708755+00:00
2023-08-05T14:16:08.708782+00:00
1,018
false
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n\n# Complexity\n- Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n\n# Code\n```\n# Write your MySQL query statement below\nSELECT * FROM PATIENTS WHERE CONDITIONS LIKE \'% DIAB1%\' OR CONDITIONS LIKE \'DIAB1%\';\n```
4
0
['MySQL']
0
patients-with-a-condition
Easy Unique Pandas Solution without Regex
easy-unique-pandas-solution-without-rege-ngid
Intuition\n Describe your first thoughts on how to solve this problem. \n\n# Approach\n Describe your approach to solving the problem. \n\n# Complexity\n- Time
deleted_user
NORMAL
2023-08-03T19:59:20.718184+00:00
2023-08-03T19:59:20.718212+00:00
540
false
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n\n# Complexity\n- Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n\n# Code\n```\nimport pandas as pd\n\ndef find_patients(patients: pd.DataFrame) -> pd.DataFrame:\n # Working Funciton have the conditions\n def working(val):\n \n length = len(val.split(" "))\n for i in range(length):\n if val.split(" ")[i][0:5] == "DIAB1": \n return True\n \n return False\n\n \n return patients[patients["conditions"].apply(lambda x: len(x)>= 5 and working(x))]\n```
4
0
['Pandas']
0
patients-with-a-condition
😢
by-kavya_10899-e64d
Intuition\n Describe your first thoughts on how to solve this problem. \n\n# Approach\n Describe your approach to solving the problem. \n\n# Complexity\n- Time
Kavya_10899
NORMAL
2023-08-01T07:15:39.273429+00:00
2023-08-01T07:15:39.273451+00:00
305
false
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n\n# Complexity\n- Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n\n# Code\n```\nimport pandas as pd\n\ndef find_patients(patients: pd.DataFrame) -> pd.DataFrame:\n filterr = patients[\'conditions\'].str.contains(r"^DIAB1") | patients[\'conditions\'].str.contains(" DIAB1")\n df=patients[filterr]\n return df\n\n```
4
0
['Pandas']
1
patients-with-a-condition
Easy SQL Query
easy-sql-query-by-yashwardhan24_sharma-7x58
Intuition\n Describe your first thoughts on how to solve this problem. \n\n# Approach\n Describe your approach to solving the problem. \n\n# Complexity\n- Time
yashwardhan24_sharma
NORMAL
2023-03-01T05:57:01.380220+00:00
2023-03-01T05:57:01.380269+00:00
1,200
false
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n\n# Complexity\n- Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n\n# Code\n```\n# Write your MySQL query statement below\nSELECT * from Patients\nWHERE conditions LIKE \'DIAB1%\'\nOR conditions LIKE \'% DIAB1%\';\n```
4
1
['MySQL']
1
patients-with-a-condition
MYSQL||SIMPLEST APPROACH
mysqlsimplest-approach-by-2stellon7-uzxa
MY SQL\n\t\tSELECT * from Patients\n\t\tWHERE\n\t\tconditions LIKE "DIAB1%" or \n\t\tconditions LIKE "% DIAB1%" #there must be space after 1st % otherwise it\
2stellon7
NORMAL
2022-10-09T16:51:15.355995+00:00
2022-10-09T16:51:15.356043+00:00
1,888
false
# MY SQL\n\t\tSELECT * from Patients\n\t\tWHERE\n\t\tconditions LIKE "DIAB1%" or \n\t\tconditions LIKE "% DIAB1%" #there must be space after 1st % otherwise it\'ll throw error\n\t\t;\n
4
0
[]
1
patients-with-a-condition
easy solution using LIKE and OR operators.
easy-solution-using-like-and-or-operator-0l5v
\nSELECT patient_id,patient_name,conditions FROM Patients WHERE conditions LIKE \'DIAB1%\' OR conditions LIKE \'% DIAB1%\';\n
akashp2001
NORMAL
2022-07-01T16:15:16.055368+00:00
2022-07-01T16:15:16.055418+00:00
410
false
```\nSELECT patient_id,patient_name,conditions FROM Patients WHERE conditions LIKE \'DIAB1%\' OR conditions LIKE \'% DIAB1%\';\n```
4
0
['MySQL']
0
patients-with-a-condition
Patients With a Condition || Easiest Solution
patients-with-a-condition-easiest-soluti-xgin
SELECT\n PATIENT_ID,\n PATIENT_NAME,\n CONDITIONS \nFROM\n PATIENTS \nWHERE\n CONDITIONS LIKE \'% DIAB1%\' \n OR CONDITIONS LIKE \'DIAB1%\' \nGROUP
mayank4513
NORMAL
2022-06-05T16:54:00.301602+00:00
2022-06-05T16:54:25.781297+00:00
429
false
SELECT\n PATIENT_ID,\n PATIENT_NAME,\n CONDITIONS \nFROM\n PATIENTS \nWHERE\n CONDITIONS LIKE \'% DIAB1%\' \n OR CONDITIONS LIKE \'DIAB1%\' \nGROUP BY\n PATIENT_ID\n \nif this helped you guys, Plzz hit the upvote button !
4
0
['String', 'MySQL']
0
patients-with-a-condition
MySQl Solution | LIKE Operator | Easy
mysql-solution-like-operator-easy-by-gur-heml
\n# Write your MySQL query statement below\nSELECT *\nFROM Patients\nWHERE conditions LIKE \'% DIAB1%\' OR conditions LIKE \'DIAB1%\'\n
gurugulati17
NORMAL
2022-05-30T16:30:25.727136+00:00
2022-05-30T16:31:07.732210+00:00
617
false
```\n# Write your MySQL query statement below\nSELECT *\nFROM Patients\nWHERE conditions LIKE \'% DIAB1%\' OR conditions LIKE \'DIAB1%\'\n```
4
0
['MySQL']
0
patients-with-a-condition
Simple OR statement
simple-or-statement-by-bat-2l70
\nselect *\nfrom Patients\nwhere \nconditions like \'DIAB1%\' or\nconditions like \'% DIAB1%\'\n\n\nUpvote if its useful!
bat_
NORMAL
2021-01-03T01:24:22.856973+00:00
2021-01-03T01:24:22.857012+00:00
640
false
```\nselect *\nfrom Patients\nwhere \nconditions like \'DIAB1%\' or\nconditions like \'% DIAB1%\'\n```\n\nUpvote if its useful!
4
0
['MySQL', 'MS SQL Server']
0
patients-with-a-condition
✅🔥💯Easy MySQL Solution || 3 Approaches || Beats - 94% || (⁠◍⁠•⁠ᴗ⁠•⁠◍⁠)
easy-mysql-solution-3-approaches-beats-9-41yq
Intuition\nThe code shows three solutions-\n- Using Wildcard Characters\n- Using LOCATE() function\n- Using POSITION() function\n\n# Code\nmysql []\nselect pati
Arcturus_22
NORMAL
2024-09-17T05:35:09.263947+00:00
2024-09-18T08:35:40.431981+00:00
396
false
# Intuition\nThe code shows three solutions-\n- Using Wildcard Characters\n- Using LOCATE() function\n- Using POSITION() function\n\n# Code\n```mysql []\nselect patient_id, patient_name, conditions\nfrom patients \nwhere conditions like \'DIAB1%\' OR\n conditions like \'% DIAB1%\';\n\n-- USING LOCATE(substring, string)\n-- where locate("DIAB1" , conditions ) = 1\n-- or locate(" DIAB1" , conditions ) <> 0;\n\n-- USING POSITION(substring IN string )\n-- where position("DIAB1" IN conditions ) = 1\n-- or position(" DIAB1" IN conditions ) <> 0;\n```\n\n**Upvote** the post if it was helpful in any way and you liked it!\nComment down for any doubts or suggestions.\n\nSigning off,\nArcturus_22\n
3
0
['Database', 'MySQL']
0
patients-with-a-condition
SQL solution beginner freindly with deep explaination.
sql-solution-beginner-freindly-with-deep-anm0
Let\'s break down the task step-by-step.\n\n## Intuition\nThe problem requires us to retrieve records of patients who have a condition starting with "DIAB1". Th
Darshan_999
NORMAL
2024-07-15T16:18:29.592161+00:00
2024-07-15T16:18:29.592180+00:00
411
false
Let\'s break down the task step-by-step.\n\n## Intuition\nThe problem requires us to retrieve records of patients who have a condition starting with "DIAB1". The wildcard character `%` is used in SQL to represent zero or more characters. By using this character in our `LIKE` clause, we can match any condition that starts with "DIAB1" or has "DIAB1" somewhere in the middle or end.\n\n## Approach\n1. **Understand the requirements**: We need to query the `Patients` table for records where the `conditions` field contains the condition "DIAB1" at the beginning or anywhere else in the string.\n2. **Formulate the SQL query**:\n - Use the `LIKE` operator to match conditions that start with "DIAB1".\n - Use `OR` to account for conditions that have "DIAB1" anywhere in the string.\n - Ensure we are checking for the space before "DIAB1" when it is not at the beginning.\n3. **Write the SQL query**: Combine the above points into a single SQL statement.\n\n## Complexity\n- **Time complexity**: The time complexity depends on the number of records in the `Patients` table. In the worst case, each row\'s `conditions` field needs to be checked, leading to a complexity of \\(O(n)\\), where \\(n\\) is the number of rows in the table.\n- **Space complexity**: The space complexity is \\(O(1)\\) as no additional space proportional to the input size is required.\n\n## Code\n```sql\nSELECT patient_id, patient_name, conditions\nFROM Patients\nWHERE conditions LIKE \'DIAB1%\' OR conditions LIKE \'% DIAB1%\';\n```\n\nThis query selects the `patient_id`, `patient_name`, and `conditions` from the `Patients` table where the `conditions` column starts with "DIAB1" or contains " DIAB1" (preceded by a space) anywhere in the string. This approach ensures we capture all relevant records based on the problem\'s requirements.
3
0
['MySQL']
0
patients-with-a-condition
Patients With a Condition || MySQL || Easy Approach
patients-with-a-condition-mysql-easy-app-0d20
Simple Approach\n\n# Approach\n1. We are selecting all the columns from patients table.\n2. For condition we will check 2 things.\n 1. if condition contain o
Dhananjay_Tripathi
NORMAL
2024-07-04T03:22:32.892876+00:00
2024-07-04T03:22:32.892909+00:00
2,544
false
# Simple Approach\n\n# Approach\n1. We are selecting all the columns from patients table.\n2. For condition we will check 2 things.\n 1. if condition contain only one word then use \'DIAB1%\'\n 2. otherwise use \'% DIAB1%\'\n\n# Code\n```\n# Write your MySQL query statement below\nselect patient_id, patient_name, conditions from Patients where conditions like \'DIAB1%\' or conditions like \'% DIAB1%\';\n```
3
0
['MySQL']
1
patients-with-a-condition
2 apporachs in MySQL
2-apporachs-in-mysql-by-suraj_raccha-ahh3
Intuition\n Describe your first thoughts on how to solve this problem. \n\n# Approach\n Describe your approach to solving the problem. \n\n# Complexity\n- Time
suraj_raccha
NORMAL
2024-03-21T09:57:24.376466+00:00
2024-03-21T09:57:24.376509+00:00
1,252
false
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n\n# Complexity\n- Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n\n# Code\nRegex Approach:\n```\n# Write your MySQL query statement below\nselect * from Patients\nwhere conditions regexp \'\\\\bDIAB1\'\n\n```\nlike keyword:\n```\nselect * from Patients\nwhere conditions like "DIAB1%" or\nconditions like \'% DIAB1%\'\n```
3
0
['MySQL']
0
patients-with-a-condition
Fast and Simple Solution
fast-and-simple-solution-by-ondecember-njz1
Intuition\nThe intuition behind this SQL query is to retrieve all rows from the "Patients" table where the "conditions" column contains the substring \'DIAB1\'
OnDecember
NORMAL
2023-12-12T17:04:02.366715+00:00
2023-12-12T17:04:02.366771+00:00
1,647
false
# Intuition\nThe intuition behind this SQL query is to retrieve all rows from the "Patients" table where the "conditions" column contains the substring \'DIAB1\' at the beginning or anywhere in the string.\n\n# Approach\nThe approach involves using the SQL SELECT statement with a WHERE clause. The conditions in the WHERE clause check for rows where the "conditions" column contains the specified substring patterns. The \'%\' symbols are used as wildcards to match any characters before or after \'DIAB1\'.\n\n# Complexity\n- Time complexity:\nThe time complexity of this query is proportional to the number of rows in the "Patients" table that satisfy the specified conditions. In the worst case, it may need to scan the entire table, resulting in a time complexity of O(n), where n is the number of rows in the table.\n\n- Space complexity:\nThe space complexity is minimal as it only involves the retrieval of rows based on a condition. It is generally considered to be O(1), constant space.\n\n# Code\n```\n# Write your MySQL query statement below\nSELECT *\nFROM Patients \nWHERE conditions LIKE (\'% DIAB1%\') OR conditions LIKE (\'DIAB1%\');\n```
3
0
['MySQL']
0
patients-with-a-condition
💻Think like SQL Engine🔥Simple Solution✅
think-like-sql-enginesimple-solution-by-gqyov
Code\n\n/* Write your T-SQL query statement below */\n\nselect patient_id, patient_name, conditions\nfrom patients \nwhere conditions like \'% DIAB1%\' or condi
k_a_m_o_l
NORMAL
2023-11-30T07:48:32.351352+00:00
2023-11-30T07:48:32.351380+00:00
1,457
false
# Code\n```\n/* Write your T-SQL query statement below */\n\nselect patient_id, patient_name, conditions\nfrom patients \nwhere conditions like \'% DIAB1%\' or conditions like \'DIAB1%\'\n```
3
0
['MS SQL Server']
1
patients-with-a-condition
Patients with a Condition in MySQL
patients-with-a-condition-in-mysql-by-lu-gqo7
Intuition\n Describe your first thoughts on how to solve this problem. \nPatients with Type I Diabetes always starts with DIAB1 prefix.\n\n# Approach\n Describe
lutfiddindev
NORMAL
2023-11-04T17:21:22.242923+00:00
2023-11-04T17:21:22.242947+00:00
373
false
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nPatients with Type I Diabetes always starts with `DIAB1` prefix.\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n> `wildcard characters` \u2192 These characters are used to substitute one or more characters in a string\n> `LIKE` \u2192 This operator searches for a specified pattern in a column\n> `OR` \u2192 This operator displays a record if any of the conditions separated by it are TRUE\n\n# Code\n```\n# Write your MySQL query statement below\nSELECT *\nFROM patients\nWHERE conditions LIKE \'% DIAB1%\' OR \nconditions LIKE \'DIAB1%\'\n```
3
0
['MySQL']
1
patients-with-a-condition
One line, no regex, pd.query solution
one-line-no-regex-pdquery-solution-by-da-6zq7
Code\n\nimport pandas as pd\n\ndef find_patients(patients: pd.DataFrame) -> pd.DataFrame:\n return patients.query("conditions.str.startswith(r\'DIAB1\') or c
daniel8b
NORMAL
2023-10-15T08:56:39.428986+00:00
2023-10-15T08:56:39.429008+00:00
254
false
# Code\n```\nimport pandas as pd\n\ndef find_patients(patients: pd.DataFrame) -> pd.DataFrame:\n return patients.query("conditions.str.startswith(r\'DIAB1\') or conditions.str.contains(r\' DIAB1\')")\n```
3
0
['Pandas']
0
patients-with-a-condition
📌 Pandas Beginner Friendly Solution 🚀
pandas-beginner-friendly-solution-by-pni-gshg
\uD83D\uDD3C IF YOU FIND THIS POST HELPFUL PLEASE UPVOTE \uD83D\uDC4D\n\nimport pandas as pd\n\ndef find_diabetes(conditions):\n \n for condition in condi
pniraj657
NORMAL
2023-08-14T15:31:23.643331+00:00
2023-08-14T15:31:23.643364+00:00
410
false
**\uD83D\uDD3C IF YOU FIND THIS POST HELPFUL PLEASE UPVOTE \uD83D\uDC4D**\n```\nimport pandas as pd\n\ndef find_diabetes(conditions):\n \n for condition in conditions.split(\' \'):\n if condition.startswith("DIAB1"):\n return True\n \n return False\n\ndef find_patients(patients: pd.DataFrame) -> pd.DataFrame:\n \n result = patients[patients[\'conditions\'].apply(find_diabetes)]\n \n return result\n```\n**Thank you for reading! \uD83D\uDE04 Comment if you have any questions or feedback.**
3
0
['Python']
1
patients-with-a-condition
3 easy MySQL solution ✅ || Pandas 1-liner✅ || Beginner-friendly ✅
3-easy-mysql-solution-pandas-1-liner-beg-brok
Intuition\n Describe your first thoughts on how to solve this problem. \n#### Solution 1\nIn solution 1, we use LIKE operator to match the substring that we nee
prathams29
NORMAL
2023-06-17T05:04:12.832299+00:00
2023-08-02T02:04:56.804942+00:00
589
false
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n#### Solution 1\nIn solution 1, we use **LIKE** operator to match the substring that we need to find. Here, we use `\'% DIAB1%\'` and `\'DIAB1%\'` because the question states that** DIAB1 will always be in the prefix so there can be a space before DIAB1 or DIAB1 is in the start of the condition**.\n\n#### Solution 2\nIn solution 2, we use **regular expression** `REGEXP \'\\\\bDIAB1\' ` where **\\b** matches either a non-word character (in our case, a space) or the position before the first character in the string.\n\n#### Solution 3\nIn solution 3, we use **regular expression** `REGEXP \'^DIAB1|[:space:]DIAB1\'` which is similar to logic of solution 1. **Either our expression starts with DIAB1 or there is a space before DIAB1**. \n\n# Code\n```\n# Solution 1\nSELECT *\nFROM Patients\nWHERE conditions LIKE \'% DIAB1%\' OR conditions LIKE \'DIAB1%\';\n\n# Soution 2 \nSELECT * \nFROM patients \nWHERE conditions REGEXP \'\\\\bDIAB1\' \n\n# Solution 3\nSELECT *\nFROM Patients\nWHERE conditions REGEXP \'^DIAB1|[:space:]DIAB1\'\n```\n```\ndef find_patients(patients: pd.DataFrame) -> pd.DataFrame:\n return patients[patients[\'conditions\'].str.contains(r\'(^DIAB1)|( DIAB1)\')]\n```
3
0
['MySQL', 'Pandas']
0
patients-with-a-condition
MySQL Solution for Patients With A Condition Problem
mysql-solution-for-patients-with-a-condi-vxpk
Intuition\n Describe your first thoughts on how to solve this problem. \nThe intuition behind the solution is to use the LIKE operator in the SQL query to match
Aman_Raj_Sinha
NORMAL
2023-06-05T12:33:45.123292+00:00
2023-06-05T12:33:45.123345+00:00
3,617
false
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nThe intuition behind the solution is to use the LIKE operator in the SQL query to match the conditions that indicate Type I Diabetes. The query searches for conditions starting with "DIAB1" (\'DIAB1%\') or conditions that have "DIAB1" preceded by a space (\'% DIAB1%\').\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\nThe approach is to retrieve the patient_id, patient_name, and conditions from the Patients table where the conditions column matches the specified patterns. By using the LIKE operator with wildcard characters (%), we can perform pattern matching and retrieve the desired results.\n\n# Complexity\n- Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\nThe time complexity of this solution depends on the size of the Patients table and the length of the conditions column. In the worst case, the query needs to scan through each row and evaluate the LIKE conditions, resulting in a linear time complexity O(N), where N is the number of rows in the table.\n\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\nThe space complexity of this solution is determined by the result set returned by the query. It will require memory to store the selected patient_id, patient_name, and conditions for the matching rows. The space complexity is typically O(M), where M is the number of rows that satisfy the conditions.\n\n# Code\n```\n# Write your MySQL query statement below\n# SELECT patient_id, patient_name, conditions\n# FROM Patients\n# WHERE conditions LIKE \'DIAB1%\'\n\nSELECT patient_id, patient_name, conditions\nFROM Patients\nWHERE conditions LIKE \'DIAB1%\' OR conditions LIKE \'% DIAB1%\'\n\n\n```
3
0
['MySQL']
1
patients-with-a-condition
Like with union
like-with-union-by-shnitsel666-a3hr
Approach\nLike with union\n\n# Code\n\n/* Write your T-SQL query statement below */\nselect patient_id, patient_name, conditions from patients\nwhere conditions
shnitsel666
NORMAL
2023-05-23T16:20:56.145607+00:00
2023-05-23T16:20:56.145661+00:00
2,339
false
# Approach\nLike with union\n\n# Code\n```\n/* Write your T-SQL query statement below */\nselect patient_id, patient_name, conditions from patients\nwhere conditions like \'DIAB1%\'\n\nunion\n\nselect patient_id, patient_name, conditions from patients\nwhere conditions like \'% DIAB1%\'\n```
3
0
['MS SQL Server']
3
patients-with-a-condition
MySQL Solution
mysql-solution-by-pranto1209-nr08
Code\n\n# Write your MySQL query statement below\nselect * from Patients \nwhere conditions like \'DIAB1%\' or conditions like \'% DIAB1%\';\n
pranto1209
NORMAL
2023-03-08T10:24:41.496344+00:00
2023-03-08T10:24:41.496389+00:00
2,266
false
# Code\n```\n# Write your MySQL query statement below\nselect * from Patients \nwhere conditions like \'DIAB1%\' or conditions like \'% DIAB1%\';\n```
3
0
['MySQL']
1
patients-with-a-condition
Optimal Solution
optimal-solution-by-divanshumehta-4i6a
\nselect*\nfrom patients\nwhere conditions like \'% DIAB1%\' or conditions like \'DIAB1%\'\n\n\n%operator specifies that there can be any number of charachters\
Divanshumehta
NORMAL
2023-01-27T08:28:39.638598+00:00
2023-01-27T08:29:28.764400+00:00
2,753
false
```\nselect*\nfrom patients\nwhere conditions like \'% DIAB1%\' or conditions like \'DIAB1%\'\n```\n\n%operator specifies that there can be any number of charachters\nlike in first condition the first % operator states there can be any number of characters before the " DIAB1" while the second one specifies that there can be any no of characters after " DIAB1".\n\nUpvote if you find it useful.
3
0
['MySQL']
3
patients-with-a-condition
mysql, where regexp <patter>
mysql-where-regexp-patter-by-nov05-erpk
https://leetcode.com/submissions/detail/857298178/\n\\\\b - MySQL word boundaries\n\n# Write your MySQL query statement below\nselect *\nfrom Patients\nwhere co
nov05
NORMAL
2022-12-09T20:57:06.966655+00:00
2022-12-09T20:57:06.966689+00:00
447
false
ERROR: type should be string, got "https://leetcode.com/submissions/detail/857298178/\\n`\\\\\\\\b` - MySQL word boundaries\\n```\\n# Write your MySQL query statement below\\nselect *\\nfrom Patients\\nwhere conditions regexp \\'\\\\\\\\bDIAB1\\'\\n```\\n\\nhttps://leetcode.com/submissions/detail/857289658/\\n```\\n# Write your MySQL query statement below\\nselect *\\nfrom Patients\\nwhere conditions like \"% DIAB1%\"\\nor conditions like \"DIAB1%\"\\n```"
3
0
['MySQL']
0
patients-with-a-condition
mysql | like
mysql-like-by-weitie-nlre
\nSELECT patient_id,\n patient_name,\n conditions\nFROM patients\nWHERE conditions LIKE \'% DIAB1%\'\n OR conditions LIKE \'DIAB1%\';\n
weitie
NORMAL
2022-09-30T00:25:57.146039+00:00
2022-09-30T00:25:57.146080+00:00
746
false
```\nSELECT patient_id,\n patient_name,\n conditions\nFROM patients\nWHERE conditions LIKE \'% DIAB1%\'\n OR conditions LIKE \'DIAB1%\';\n```
3
0
[]
0
patients-with-a-condition
[Oracle] Wildcard and Regex solution, explain plan review.
oracle-wildcard-and-regex-solution-expla-8m53
1.Wildcard\n\nSELECT\n pa.patient_id\n , pa.patient_name\n , pa.conditions\nFROM patients pa\nWHERE\n pa.conditions LIKE \'DIAB1%\'\nOR\n pa.cond
duynl
NORMAL
2022-08-11T13:58:36.358911+00:00
2022-08-11T13:58:36.358955+00:00
302
false
**1.Wildcard**\n```\nSELECT\n pa.patient_id\n , pa.patient_name\n , pa.conditions\nFROM patients pa\nWHERE\n pa.conditions LIKE \'DIAB1%\'\nOR\n pa.conditions LIKE \'% DIAB1%\'\n;\n```\n![image](https://assets.leetcode.com/users/images/e60f4e03-c952-4a65-81f2-83d4e5624bef_1660225907.5812278.png)\n\n**2.Regex**\n```\nSELECT\n pa.patient_id\n , pa.patient_name\n , pa.conditions\nFROM \n patients pa\nWHERE\n REGEXP_LIKE(pa.conditions, \'^DIAB1|\\sDIAB1\')\n;\n```\n- `^DIAB1`: conditions begin with DIAB1 (same as LIKE \'DIAB1%\')\n- `|`: OR\n- `\\sDIAB1`: conditions begin with whitespace+DIAB1 (same as LIKE \'% DIAB1%\')\n![image](https://assets.leetcode.com/users/images/db7a4ecd-6fb6-440c-b0d9-fa4962c73afc_1660226010.979697.png)\n\nBoth queries have the same explain plan. Wildcard is more straigh forward, but Regex is more simple.
3
0
['Oracle']
1
patients-with-a-condition
Oracle Simple & Fastest Solution
oracle-simple-fastest-solution-by-ruthvi-8zuf
Please upvote if you find it helpful, thank you.\n\n\n/* Write your PL/SQL query statement below */\n\nSELECT * FROM patients\nWHERE conditions LIKE \'% DIAB1%\
ruthvikc27-dev
NORMAL
2022-07-17T23:50:07.064567+00:00
2022-07-17T23:50:07.064610+00:00
488
false
# Please upvote if you find it helpful, thank you.\n\n```\n/* Write your PL/SQL query statement below */\n\nSELECT * FROM patients\nWHERE conditions LIKE \'% DIAB1%\' OR conditions LIKE \'DIAB1%\';\n```
3
0
['MySQL', 'Oracle']
0
patients-with-a-condition
Easy MSSQL solution
easy-mssql-solution-by-anuragsrawat-3kea
sql\nSELECT * FROM Patients\nWHERE conditions LIKE \'DIAB1%\' OR \n\t conditions LIKE \'% DIAB1%\'\n
anuragsrawat
NORMAL
2022-06-07T16:51:50.048512+00:00
2022-06-07T16:51:50.048542+00:00
555
false
```sql\nSELECT * FROM Patients\nWHERE conditions LIKE \'DIAB1%\' OR \n\t conditions LIKE \'% DIAB1%\'\n```
3
0
['MySQL', 'MS SQL Server']
0
patients-with-a-condition
Easiest Solution | MySQL
easiest-solution-mysql-by-_dhruvbhatia-whka
Pls Upvote if you like the solution!\n```\nSELECT * FROM PATIENTS WHERE CONDITIONS LIKE \'% DIAB1%\' OR CONDITIONS LIKE \'DIAB1%\';
_dhruvbhatia_
NORMAL
2022-05-27T08:53:32.721237+00:00
2022-05-27T08:53:32.721280+00:00
447
false
**Pls Upvote if you like the solution!**\n```\nSELECT * FROM PATIENTS WHERE CONDITIONS LIKE \'% DIAB1%\' OR CONDITIONS LIKE \'DIAB1%\';
3
0
['MySQL']
0
patients-with-a-condition
Super easy, faster than 93%
super-easy-faster-than-93-by-aldata-yco2
select * from Patients where conditions like \'DIAB1%\' or conditions like \'% DIAB1%\'
ALDATA
NORMAL
2022-05-06T08:58:08.952514+00:00
2022-05-06T08:58:08.952548+00:00
515
false
select * from Patients where conditions like \'DIAB1%\' or conditions like \'% DIAB1%\'
3
0
[]
3
patients-with-a-condition
MySQL elegant solution
mysql-elegant-solution-by-turael-hext
\nSELECT\n patient_id,\n patient_name,\n conditions\nFROM Patients\nWHERE conditions REGEXP "\\\\bDIAB1"\n
turael
NORMAL
2022-04-21T13:03:22.692026+00:00
2022-04-21T13:03:22.692059+00:00
256
false
```\nSELECT\n patient_id,\n patient_name,\n conditions\nFROM Patients\nWHERE conditions REGEXP "\\\\bDIAB1"\n```
3
0
['MySQL']
0
patients-with-a-condition
✅ [Accepted] Solution for MySQL
accepted-solution-for-mysql-by-asahiocea-dkz9
sql\nSELECT * FROM PATIENTS WHERE\nCONDITIONS LIKE \'% DIAB1%\' OR CONDITIONS LIKE \'DIAB1%\';\n
AsahiOcean
NORMAL
2022-04-09T18:19:28.264895+00:00
2022-04-10T12:08:21.028974+00:00
1,347
false
```sql\nSELECT * FROM PATIENTS WHERE\nCONDITIONS LIKE \'% DIAB1%\' OR CONDITIONS LIKE \'DIAB1%\';\n```
3
0
['MySQL']
0
patients-with-a-condition
Easy for beginner
easy-for-beginner-by-_oviya-4065
Code
_oviya_
NORMAL
2025-04-08T12:11:31.315205+00:00
2025-04-08T12:11:31.315205+00:00
288
false
# Code ```mysql [] # Write your MySQL query statement below select patient_id , patient_name , conditions from Patients where conditions like 'DIAB1%' or conditions like '% DIAB1%'; ```
2
0
['Database', 'MySQL']
0
patients-with-a-condition
MySQL solution using LIKE
mysql-solution-using-like-by-swapit-w986
Code
swapit
NORMAL
2025-03-02T19:22:58.643169+00:00
2025-03-02T19:22:58.643169+00:00
631
false
# Code ```mysql [] # Write your MySQL query statement below SELECT patient_id,patient_name,conditions FROM Patients WHERE conditions LIKE 'DIAB1%' OR conditions LIKE '% DIAB1%'; ```
2
0
['MySQL']
0
patients-with-a-condition
Easy solution with 73% beats ||
easy-solution-with-73-beats-by-ritikg436-ny47
IntuitionApproachComplexity Time complexity: Space complexity: Code
ritikg4360
NORMAL
2025-02-01T10:56:35.615646+00:00
2025-02-01T10:56:35.615646+00:00
501
false
# Intuition <!-- Describe your first thoughts on how to solve this problem. --> # Approach <!-- Describe your approach to solving the problem. --> # Complexity - Time complexity: <!-- Add your time complexity here, e.g. $$O(n)$$ --> - Space complexity: <!-- Add your space complexity here, e.g. $$O(n)$$ --> # Code ```mysql [] select * from patients where conditions like 'DIAB1%' or conditions like '% DIAB1%' ; ```
2
0
['Database', 'MySQL']
0
patients-with-a-condition
✅✅Beats 99.9%🔥MySQL🔥|| 🚀🚀Super Simple and Efficient Solution🚀🚀||🔥🔥MySQL✅✅
beats-999mysql-super-simple-and-efficien-r94y
Code
shobhit_yadav
NORMAL
2025-01-06T08:08:00.115607+00:00
2025-01-06T08:08:00.115607+00:00
694
false
# Code ```mysql [] # Write your MySQL query statement below SELECT * FROM Patients WHERE conditions LIKE 'DIAB1%' OR conditions LIKE '% DIAB1%'; ```
2
0
['Database', 'MySQL']
1
patients-with-a-condition
postgresql
postgresql-by-victorprokhorov-4rsh
sql\nSELECT patient_id,\n patient_name,\n conditions\nFROM patients\nWHERE conditions LIKE \'DIAB1%\'\n OR conditions LIKE \'% DIAB1%\'\n\
victorprokhorov
NORMAL
2024-11-17T20:57:38.077665+00:00
2024-11-17T20:57:38.077692+00:00
257
false
```sql\nSELECT patient_id,\n patient_name,\n conditions\nFROM patients\nWHERE conditions LIKE \'DIAB1%\'\n OR conditions LIKE \'% DIAB1%\'\n```\n\n```sql\nSELECT patient_id,\n patient_name,\n conditions\nFROM patients\nWHERE conditions ~ \'^DIAB1| DIAB1\' \n```
2
0
['PostgreSQL']
0
patients-with-a-condition
MySQL
mysql-by-siyadhri-nicp
\n\n# Code\nmysql []\n# Write your MySQL query statement below\n\nselect patient_id,patient_name,conditions from Patients\nwhere conditions like \'DIAB1%\' or
siyadhri
NORMAL
2024-09-30T04:53:17.471729+00:00
2024-09-30T04:53:17.471787+00:00
195
false
\n\n# Code\n```mysql []\n# Write your MySQL query statement below\n\nselect patient_id,patient_name,conditions from Patients\nwhere conditions like \'DIAB1%\' or conditions like \'% DIAB1%\' ;\n```
2
0
['MySQL']
0
patients-with-a-condition
sql
sql-by-yashikart-orjm
Intuition\n Describe your first thoughts on how to solve this problem. \n\n# Approach\n Describe your approach to solving the problem. \n\n# Complexity\n- Time
yashikaRT
NORMAL
2024-08-09T15:43:47.027708+00:00
2024-08-09T15:43:47.027740+00:00
1,407
false
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n\n# Complexity\n- Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n\n# Code\n```\n# Write your MySQL query statement below\nSELECT * FROM Patients\nWHERE conditions LIKE \'% DIAB1%\' -- Code starts with DIAB1 and is not the first code\n OR conditions LIKE \'DIAB1%\' -- Code starts with DIAB1 and is the first code\n OR conditions LIKE \'% DIAB1\' -- Code starts with DIAB1 and is the last code\n OR conditions = \'DIAB1\'\n```
2
0
['MySQL']
1
patients-with-a-condition
ORACLE SOULTION | Beats 97.22%
oracle-soultion-beats-9722-by-shivangish-f9bp
\n# Code\n\n/* Write your PL/SQL query statement below */\nselect * from patients\nwhere conditionS like \'% DIAB1%\' OR conditionS like \'DIAB1%\' ;\n
shivangisharma94
NORMAL
2024-06-13T09:21:46.689184+00:00
2024-06-13T09:21:46.689236+00:00
1,240
false
\n# Code\n```\n/* Write your PL/SQL query statement below */\nselect * from patients\nwhere conditionS like \'% DIAB1%\' OR conditionS like \'DIAB1%\' ;\n```
2
0
['Oracle']
1
patients-with-a-condition
Beginner-friendly Solution Using SQL || Beats 96% Users || Clear
beginner-friendly-solution-using-sql-bea-e9e6
\n# Code\n\n# Write your MySQL query statement below\nselect patient_id, patient_name, conditions\nfrom Patients\nwhere conditions like \'DIAB1%\' or conditions
truongtamthanh2004
NORMAL
2024-05-18T13:33:00.494219+00:00
2024-05-18T13:33:00.494254+00:00
821
false
\n# Code\n```\n# Write your MySQL query statement below\nselect patient_id, patient_name, conditions\nfrom Patients\nwhere conditions like \'DIAB1%\' or conditions like \'% DIAB1%\'\n```
2
0
['MySQL']
1
patients-with-a-condition
2 Solutions (LIKE and REGEXP) || MySQL simple solution || 78.67% Faster
2-solutions-like-and-regexp-mysql-simple-rt8q
Approach\nThere are 2 solutions provided, 1 is commented out and the other below that. \n1. The one in comment is simple, the query selects all the columns wher
Aditibhutada
NORMAL
2024-03-17T19:18:22.637053+00:00
2024-03-17T19:18:22.637087+00:00
897
false
# Approach\nThere are 2 solutions provided, 1 is commented out and the other below that. \n1. The one in comment is simple, the query selects all the columns where either the condition starts with \'DIAB1%\' and can have any character after that OR it has a space before DIAB1 i.e. \'% DIAB1%\' indicating that it is a new word to avoid cases like \'SADIAB100\'\n\n2. The second solution is an abbreviation of first using **REGEXP** expression where \'\\b\' is a word boundary anchor in regular expressions.It represents a position between a word character (like letters, digits, and underscores) and a non-word character (like spaces, punctuation, or the start/end of the string). \n**Here we use \'\\\\\\b\' as the first backlash is used to escape the \'\\b\'**\n\n# Code\n```\n# Write your MySQL query statement below\n-- SELECT * FROM Patients WHERE conditions LIKE "% DIAB1%" OR conditions LIKE "DIAB1%";\nSELECT * FROM Patients WHERE conditions REGEXP "\\\\bDIAB1";\n```
2
0
['Database', 'MySQL']
1
patients-with-a-condition
100 % || INTUITIVE && WELL - EXPLAINED || EASY SQL QUERY || Using LIKE Opr. || Beats 92.33 % || Ex.
100-intuitive-well-explained-easy-sql-qu-u7hu
Intuition\n Describe your first thoughts on how to solve this problem. \nSELECT * --> show all records from Patients table...\nFROM Patients --> Association fro
ganpatinath07
NORMAL
2024-02-14T16:25:10.592678+00:00
2024-02-14T16:25:10.592703+00:00
1,194
false
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nSELECT * --> **show** all records from **Patients** table...\nFROM Patients --> **Association** from **Patients** table...\nWHERE conditions LIKE (\'DIAB1%\') --> Prefix should be **\'DIAB1\'**...\nOR conditions LIKE (\'% DIAB1%\'); --> also for second word, Prefix should be **\'DIAB1\'**...\n# Approach\n<!-- Describe your approach to solving the problem. -->\nUsing Fundamentals of **SQL**...\n# Complexity\n- Time complexity: O(n)\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity: O(1)\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n\n# Code\n```\nSELECT * \nFROM Patients \nWHERE conditions LIKE (\'DIAB1%\') \nOR conditions LIKE (\'% DIAB1%\');\n```
2
0
['Database', 'MySQL']
1
patients-with-a-condition
Clear solution My SQL || SQLite ✅
clear-solution-my-sql-sqlite-by-sardorbe-cigd
Patients With a Condition\uD83D\uDD25\uD83D\uDCAF\n- This part specifies the columns that will be included in the result set. In this case, it includes patient_
SardorbekBahramov
NORMAL
2023-12-25T06:04:50.711614+00:00
2023-12-25T06:04:50.711649+00:00
1,689
false
# Patients With a Condition\uD83D\uDD25\uD83D\uDCAF\n- This part specifies the columns that will be included in the result set. In this case, it includes patient_id, patient_name, and conditions.\n```\nSELECT patient_id, patient_name, conditions\n```\n- This part indicates the source table from which data will be retrieved. In this case, it\'s the Patients table.\n```\nFROM Patients\n```\n- The WHERE clause is used to filter the rows based on a specified condition. Here, it filters rows where the conditions column starts with the "DIAB1" prefix (LIKE \'DIAB1%\') or has "DIAB1" preceded by a space (LIKE \'% DIAB1%\').\n```\nWHERE conditions LIKE \'DIAB1%\' OR conditions LIKE \'% DIAB1%\';\n```\n> ### The result is a set of rows containing patient_id, patient_name, and conditions for patients who have Type I Diabetes, based on the specified conditions in the WHERE clause.\n> In summary, this query retrieves information from the Patients table for patients with Type I Diabetes, where the conditions column either starts with "DIAB1" or has "DIAB1" preceded by a space.\n\n```\nSELECT patient_id, patient_name, conditions\nFROM Patients\nWHERE conditions LIKE \'DIAB1%\' OR conditions LIKE \'% DIAB1%\';\n```\n\n\n\n\n\n
2
0
[]
0
patients-with-a-condition
EASY MYSQL SOLUTION
easy-mysql-solution-by-2005115-qgix
PLEASE UPVOTE MY SOLUTION IG YOU LIKE IT\n# CONNECT WITH ME\n# https://www.linkedin.com/in/pratay-nandy-9ba57b229/\n# https://www.instagram.com/pratay_nandy/\n#
2005115
NORMAL
2023-09-11T10:21:08.724556+00:00
2023-09-11T10:21:08.724577+00:00
658
false
# **PLEASE UPVOTE MY SOLUTION IG YOU LIKE IT**\n# **CONNECT WITH ME**\n# **[https://www.linkedin.com/in/pratay-nandy-9ba57b229/]()**\n# **[https://www.instagram.com/pratay_nandy/]()**\n# Approach\n<!-- Describe your approach to solving the problem. -->\nSELECT * FROM patients: This part of the query selects all columns (*) from the "patients" table.\n\nWHERE conditions REGEXP \'\\bDIAB1\': This part of the query filters the results based on a condition. It checks the "conditions" column to see if it matches the regular expression \'\\bDIAB1\'.\n\n\'\\b\' is a word boundary anchor in regular expressions. It ensures that "DIAB1" is treated as a whole word and not as part of another word.\n\n\'DIAB1\' is the pattern being matched. This means it\'s looking for the exact string "DIAB1" within the "conditions" column.\n\nSo, this query will return all rows from the "patients" table where the "conditions" column contains the word "DIAB1."\n\n# **Code**\n```\nSELECT * FROM patients WHERE conditions REGEXP \'\\\\bDIAB1\'\n```
2
0
['Database', 'MySQL']
1
patients-with-a-condition
Simple and Easy Solution !!!
simple-and-easy-solution-by-kumaravelmal-bkk5
Code\n\n/* Write your PL/SQL query statement below */\nselect patient_id, patient_name, conditions from patients where upper(conditions) like \'DIAB1%\' OR uppe
kumaravelmalayappan
NORMAL
2023-09-08T16:12:41.621247+00:00
2023-09-08T16:12:41.621296+00:00
189
false
# Code\n```\n/* Write your PL/SQL query statement below */\nselect patient_id, patient_name, conditions from patients where upper(conditions) like \'DIAB1%\' OR upper(conditions) like \'% DIAB1%\'\n```
2
0
['MySQL', 'Oracle', 'MS SQL Server']
0
patients-with-a-condition
EASY AND SIMPLE MYSQL QUERY
easy-and-simple-mysql-query-by-kaushal_s-xysb
\n\n# Code\n\n# Write your MySQL query statement below\nSELECT *FROM Patients\nWHERE conditions LIKE \'% DIAB1%\' OR conditions LIKE \'DIAB1%\'\n
Kaushal_Surana
NORMAL
2023-08-28T13:02:02.589561+00:00
2023-08-28T13:02:02.589589+00:00
852
false
\n\n# Code\n```\n# Write your MySQL query statement below\nSELECT *FROM Patients\nWHERE conditions LIKE \'% DIAB1%\' OR conditions LIKE \'DIAB1%\'\n```
2
0
['MySQL']
0
patients-with-a-condition
Simple Solution with str.contains
simple-solution-with-strcontains-by-chv5-zi1k
Intuition\n Describe your first thoughts on how to solve this problem. \n\n# Approach\n Describe your approach to solving the problem. \n\n# Complexity\n- Time
chv532
NORMAL
2023-08-10T16:16:33.349910+00:00
2023-08-10T16:16:33.349938+00:00
1,097
false
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n\n# Complexity\n- Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n\n# Code\n```\nimport pandas as pd\n\ndef find_patients(patients: pd.DataFrame) -> pd.DataFrame:\n pc_df = patients[patients["conditions"].str.contains(r"(^DIAB1)|( DIAB1)")]\n return pc_df\n```
2
0
['Pandas']
1
patients-with-a-condition
Simple Logic 🎈
simple-logic-by-ravithemore-v0jr
\n# Approach\nThis Question is simple one but has one simple logic which is confusing a lot. \nSELECT * FROM PATIENTS WHERE\nCONDITIONS LIKE \'% DIAB1%\' OR CON
ravithemore
NORMAL
2023-07-24T13:00:24.372683+00:00
2023-07-24T13:00:24.372703+00:00
39
false
\n# Approach\nThis Question is simple one but has one simple logic which is confusing a lot. \n`SELECT * FROM PATIENTS WHERE\nCONDITIONS LIKE \'% DIAB1%\' OR CONDITIONS LIKE \'DIAB1%\';`\nThis Code will work for all the test cases but will fail for the last one because in it `DIAB1` is there but it in the between and we have to return null but this condition `%DIAB1%` will not give null so You have to just add `\'% DIAB1%\'` space before the word so the it will check only for the word where there is space at the Starting.\n# Code\n```\n# Write your MySQL query statement below\nSELECT * FROM PATIENTS WHERE\nCONDITIONS LIKE \'% DIAB1%\' OR\nCONDITIONS LIKE \'DIAB1%\';\n\n```\n![1qivbn.jpg](https://assets.leetcode.com/users/images/a169dbd7-b34d-4a99-8359-a8a813239d9a_1689869293.2703924.jpeg)
2
0
['MySQL']
0
patients-with-a-condition
Using regex and like
using-regex-and-like-by-saravanakumaran_-awkl
Approach\nRegex to exclude intermediate similar strings\n\n\n# Code\n\nselect \n * \nfrom Patients \nwhere conditions like \'%[^A-Z]DIAB1%\' or\n conditi
saravanakumaran_m
NORMAL
2023-06-11T14:14:42.859507+00:00
2023-06-11T14:14:42.859551+00:00
218
false
# Approach\nRegex to exclude intermediate similar strings\n\n\n# Code\n```\nselect \n * \nfrom Patients \nwhere conditions like \'%[^A-Z]DIAB1%\' or\n conditions like \'DIAB1%\'\n```
2
0
['MS SQL Server']
0
patients-with-a-condition
Easy solution
easy-solution-by-shubhi4-8nmc
Intuition\n Describe your first thoughts on how to solve this problem. \n\n# Approach\n Describe your approach to solving the problem. \n\n# Complexity\n- Time
shubhi4
NORMAL
2023-04-03T10:42:08.797282+00:00
2023-04-03T10:42:08.797332+00:00
830
false
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n\n# Complexity\n- Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n\n# Code\n```\n/* Write your PL/SQL query statement below */\n\nselect * from patients where conditions like \'% DIAB1%\' or conditions like \'DIAB1%\'\n```
2
0
['Oracle']
0
identify-the-largest-outlier-in-an-array
Well Explained, Beginner Friendly Solution !!! O(n) Solution !!! Beats 100% Solutions !!!
well-explained-beginner-friendly-solutio-jzu6
Intuition\n\nTo solve this problem, we need to find the largest potential outlier in an array where:\n- \( n - 2 \) elements are special numbers.\n- One element
deepan484
NORMAL
2024-12-02T17:10:30.285244+00:00
2024-12-02T17:12:39.652437+00:00
6,384
false
### Intuition\n\nTo solve this problem, we need to find the largest potential outlier in an array where:\n- \\( n - 2 \\) elements are special numbers.\n- One element is the sum of the special numbers.\n- One element is the outlier, which is distinct from both the sum element and the special numbers.\n\nThe problem hints that we can leverage the relationship between the sum of all elements and the sum of special numbers. Specifically, the sum of all elements in the array is related to the sum of the special numbers, their sum, and the outlier. This relationship can be described by the equation:\n\n$$\n\\text{total\\_sum} = 2 \\times (\\text{sum of special numbers}) + (\\text{outlier})\n$$\n\nThe task is to find the largest outlier, given that there is one candidate that is the sum of the special numbers and the rest are special numbers.\n\n---\n\n### Approach\n\nTo solve this efficiently, I\u2019ll break down the approach as follows:\n\n1. **Calculate the Total Sum of All Elements**: \n First, compute the sum of all elements in the array (`total_sum`). This will help us later in identifying potential outliers.\n\n2. **Count Frequencies of Elements**: \n Use a dictionary (`num_counts`) to track the frequency of each number in the array. This helps in quickly checking whether a number exists in the array when needed.\n\n3. **Identify Possible Outliers**: \n For each unique number `num` in the array, treat it as a candidate for the sum of the special numbers. The potential outlier is calculated using the formula:\n $$\n \\text{potential\\_outlier} = \\text{total\\_sum} - 2 \\times \\text{num}\n $$\n This formula comes from the equation:\n $$\n \\text{outlier} = \\text{total\\_sum} - 2 \\times \\text{sum of special numbers}\n $$\n\n4. **Check Validity of Potential Outlier**: \n For each candidate number, check if the calculated potential outlier exists in the array. Ensure that the outlier is distinct from the candidate number, unless the candidate appears multiple times in the array.\n\n5. **Track the Largest Outlier**: \n Throughout the iteration, keep track of the largest valid outlier and return that as the result.\n\n---\n\n### Complexity\n\n- **Time complexity**: \n - Calculating the total sum: \\(O(n)\\), where \\(n\\) is the number of elements in the array.\n - Constructing the frequency map: \\(O(n)\\).\n - Iterating over the unique keys in the frequency map: \\(O(m)\\), where \\(m\\) is the number of unique elements, and \\(m \\leq n\\). \n - Therefore, the overall time complexity is \\(O(n)\\).\n\n- **Space complexity**: \n The space complexity is dominated by the space used for the frequency map, which stores the count of each unique number. Hence, the space complexity is \\(O(m)\\), where \\(m\\) is the number of unique elements in the array.\n\n---\n\n### Code\n\n```python\n\nclass Solution:\n def getLargestOutlier(self, nums: List[int]) -> int:\n \n total_sum = sum(nums) \n \n num_counts = defaultdict(int)\n for num in nums:\n num_counts[num] += 1\n \n\n largest_outlier = float(\'-inf\')\n \n \n for num in num_counts.keys():\n \n potential_outlier = total_sum - 2 * num \n \n \n if potential_outlier in num_counts:\n if potential_outlier != num or num_counts[num] > 1: \n largest_outlier = max(largest_outlier, potential_outlier)\n \n \n return largest_outlier\n
55
0
['Python3']
11
identify-the-largest-outlier-in-an-array
Check if Sum of all remaining equal double of one item - Explained
check-if-sum-of-all-remaining-equal-doub-khr8
Intution \n- The obivious intution we come accross is do some sort of presum and then evaluate the total sum by skipping outlier\n- Also i thought that => the s
kreakEmp
NORMAL
2024-12-01T04:39:24.726179+00:00
2024-12-05T06:19:40.684921+00:00
9,454
false
# Intution \n- The obivious intution we come accross is do some sort of presum and then evaluate the total sum by skipping outlier\n- Also i thought that => the sum of the remaing will be highest one only and need to check the sum of others and compare it with the largest one but this leads to lots of corener cases and even later realised that it is not true ( ex. [-4, -2, 5, 9] )\n- Later i realise the following after almost spending 30min in resolving above corner cases.\n- If we consider one item as the outlier and when skipped then for the remaining items => \n total sum is two times the sum of one of the items we considered =>\n we need to take all sum and remove the current item ( outlier item) and then the sum should be double of one of the items in the considered items.\n \n Why this works ? \n - consider you have a list of items ( a, b, c ) and one of them is the sum of remaing.\n - Lets assume a +b = c. So when we take sum of all items => a + b +c = c + c = 2c\n - Which shows that the total sum is double of the one of the item.\n \n- One corner case is => when the double of the total sum is equal to the outlier item\n - then in this case we need to check its freq => if greater then equal to 2 that means even we remove one of them as outlier we have at least one more which can be the sum of remaining.\n\n\n# Approach\n- Take sum of all items\n- Also put twice of each item in the map and count freq\n- Now iterate over each items and consider as the out lier \n - if this is true then the sum of remaining items should be found in the map\n - Also if the sum of all remaining items equal to our outlier item then we need to check if its freq in map is greater then 2 or not.\n - Keep tracking the max possible item and return it as ans\n\n# Complexity :\n- Time complexity : O(N)\n- Space complexity : O(N)\n\n# Code\n```\nint getLargestOutlier(vector<int>& nums) {\n int ans = INT_MIN, sum = 0;\n unordered_map<int, int> freq;\n for(auto n : nums){\n sum += n;\n freq[n*2]++;\n }\n for(auto n: nums){\n int t = sum - n;\n if(freq[t] >= 2 || (freq[t] == 1) && (t != n*2)) ans = max(ans, n);\n }\n return ans;\n}\n```\n\n---\n\n\n<b>Here is an article of my interview experience - A Journey to FAANG Company, I recomand you to go through this to know which all resources I have used & how I cracked interview at Amazon:\nhttps://leetcode.com/discuss/interview-experience/3171859/Journey-to-a-FAANG-Company-Amazon-or-SDE2-(L5)-or-Bangalore-or-Oct-2022-Accepted\n\n\n---\n
46
1
['C++']
10
identify-the-largest-outlier-in-an-array
[Java/C++/Python] HashMap
javacpython-hashmap-by-lee215-9npa
Explanation\nCount elements total sum and frequence count.\nFor each a as the sum,\nwe check outlier = total - a - a,\nif outlier exists,\nwe update res = max(r
lee215
NORMAL
2024-12-01T04:34:12.063689+00:00
2024-12-08T12:42:14.583142+00:00
6,154
false
# **Explanation**\nCount elements `total` sum and frequence `count`.\nFor each `a` as the sum,\nwe check `outlier = total - a - a`,\nif `outlier` exists,\nwe update `res = max(res, outlier)`.\n\nfinally return result `res`.\n\n# **Complexity**\nTime `O(n)`\nSpace `O(n)`\n<br>\n\n```Java [Java]\n public int getLargestOutlier(int[] A) {\n Map<Integer, Integer> count = new HashMap<>();\n int total = 0, res = Integer.MIN_VALUE;\n for (int a : A) {\n total += a;\n count.put(a, count.getOrDefault(a, 0) + 1);\n }\n for (int a : A) {\n int outlier = total - a - a;\n if (count.getOrDefault(outlier, 0) > (outlier == a ? 1 : 0)) {\n res = Math.max(res, outlier);\n }\n }\n return res;\n }\n```\n\n```C++ [C++]\n int getLargestOutlier(vector<int>& A) {\n unordered_map<int, int> count;\n int total = 0, res = INT_MIN;\n for (int a : A) {\n total += a;\n count[a]++;\n }\n for (int a : A) {\n int outlier = total - a - a;\n if (count[outlier] > (outlier == a)) {\n res = max(res, outlier);\n }\n }\n return res;\n }\n```\n\n```py [Python3]\n def getLargestOutlier(self, A: List[int]) -> int:\n total = sum(A)\n count = Counter(A)\n res = -inf\n for a in A:\n outlier = total - a - a\n if count[outlier] > (outlier == a):\n res = max(res, outlier)\n return res\n```\n
25
2
['C', 'Python', 'Java']
6