|
| 1 | +--- |
| 2 | +comments: true |
| 3 | +difficulty: Easy |
| 4 | +edit_url: https://github.com/doocs/leetcode/edit/main/solution/3100-3199/3198.Find%20Cities%20in%20Each%20State/README_EN.md |
| 5 | +--- |
| 6 | + |
| 7 | +<!-- problem:start --> |
| 8 | + |
| 9 | +# [3198. Find Cities in Each State 🔒](https://leetcode.com/problems/find-cities-in-each-state) |
| 10 | + |
| 11 | +[中文文档](/solution/3100-3199/3198.Find%20Cities%20in%20Each%20State/README.md) |
| 12 | + |
| 13 | +## Description |
| 14 | + |
| 15 | +<!-- description:start --> |
| 16 | + |
| 17 | +<p>Table: <code>cities</code></p> |
| 18 | + |
| 19 | +<pre> |
| 20 | ++-------------+---------+ |
| 21 | +| Column Name | Type | |
| 22 | ++-------------+---------+ |
| 23 | +| state | varchar | |
| 24 | +| city | varchar | |
| 25 | ++-------------+---------+ |
| 26 | +(state, city) is the primary key (combination of columns with unique values) for this table. |
| 27 | +Each row of this table contains the state name and the city name within that state. |
| 28 | +</pre> |
| 29 | + |
| 30 | +<p>Write a solution to find <strong>all the cities in each state</strong> and combine them into a <strong>single comma-separated</strong> string.</p> |
| 31 | + |
| 32 | +<p>Return <em>the result table ordered by</em> <code>state</code> <em>in <strong>ascending</strong> order</em>.</p> |
| 33 | + |
| 34 | +<p>The result format is in the following example.</p> |
| 35 | + |
| 36 | +<p> </p> |
| 37 | +<p><strong class="example">Example:</strong></p> |
| 38 | + |
| 39 | +<div class="example-block"> |
| 40 | +<p><strong>Input:</strong></p> |
| 41 | + |
| 42 | +<p>cities table:</p> |
| 43 | + |
| 44 | +<pre class="example-io"> |
| 45 | ++-------------+---------------+ |
| 46 | +| state | city | |
| 47 | ++-------------+---------------+ |
| 48 | +| California | Los Angeles | |
| 49 | +| California | San Francisco | |
| 50 | +| California | San Diego | |
| 51 | +| Texas | Houston | |
| 52 | +| Texas | Austin | |
| 53 | +| Texas | Dallas | |
| 54 | +| New York | New York City | |
| 55 | +| New York | Buffalo | |
| 56 | +| New York | Rochester | |
| 57 | ++-------------+---------------+ |
| 58 | +</pre> |
| 59 | + |
| 60 | +<p><strong>Output:</strong></p> |
| 61 | + |
| 62 | +<pre class="example-io"> |
| 63 | ++-------------+---------------------------------------+ |
| 64 | +| state | cities | |
| 65 | ++-------------+---------------------------------------+ |
| 66 | +| California | Los Angeles, San Diego, San Francisco | |
| 67 | +| New York | Buffalo, New York City, Rochester | |
| 68 | +| Texas | Austin, Dallas, Houston | |
| 69 | ++-------------+---------------------------------------+ |
| 70 | +</pre> |
| 71 | + |
| 72 | +<p><strong>Explanation:</strong></p> |
| 73 | + |
| 74 | +<ul> |
| 75 | + <li><strong>California:</strong> All cities ("Los Angeles", "San Diego", "San Francisco") are listed in a comma-separated string.</li> |
| 76 | + <li><strong>New York:</strong> All cities ("Buffalo", "New York City", "Rochester") are listed in a comma-separated string.</li> |
| 77 | + <li><strong>Texas:</strong> All cities ("Austin", "Dallas", "Houston") are listed in a comma-separated string.</li> |
| 78 | +</ul> |
| 79 | + |
| 80 | +<p><strong>Note:</strong> The output table is ordered by the state name in ascending order.</p> |
| 81 | +</div> |
| 82 | + |
| 83 | +<!-- description:end --> |
| 84 | + |
| 85 | +## Solutions |
| 86 | + |
| 87 | +<!-- solution:start --> |
| 88 | + |
| 89 | +### Solution 1: Grouping and Aggregation |
| 90 | + |
| 91 | +We can first group by the `state` field, then sort the `city` field within each group, and finally use the `GROUP_CONCAT` function to concatenate the sorted city names into a comma-separated string. |
| 92 | + |
| 93 | +<!-- tabs:start --> |
| 94 | + |
| 95 | +#### MySQL |
| 96 | + |
| 97 | +```sql |
| 98 | +# Write your MySQL query statement below |
| 99 | +SELECT |
| 100 | + state, |
| 101 | + GROUP_CONCAT(city ORDER BY city SEPARATOR ', ') cities |
| 102 | +FROM cities |
| 103 | +GROUP BY 1 |
| 104 | +ORDER BY 1; |
| 105 | +``` |
| 106 | + |
| 107 | +#### Pandas |
| 108 | + |
| 109 | +```python |
| 110 | +import pandas as pd |
| 111 | + |
| 112 | + |
| 113 | +def find_cities(cities: pd.DataFrame) -> pd.DataFrame: |
| 114 | + result = ( |
| 115 | + cities.groupby("state")["city"] |
| 116 | + .apply(lambda x: ", ".join(sorted(x))) |
| 117 | + .reset_index() |
| 118 | + ) |
| 119 | + result.columns = ["state", "cities"] |
| 120 | + return result |
| 121 | +``` |
| 122 | + |
| 123 | +<!-- tabs:end --> |
| 124 | + |
| 125 | +<!-- solution:end --> |
| 126 | + |
| 127 | +<!-- problem:end --> |
0 commit comments