Find Cities in Each State II Problem
Description
LeetCode Problem 3328.
Table: cities
+-------------+---------+
| Column Name | Type |
+-------------+---------+
| state | varchar |
| city | varchar |
+-------------+---------+
(state, city) is the combination of columns with unique values for this table.
Each row of this table contains the state name and the city name within that state.
Write a solution to find all the cities in each state and analyze them based on the following requirements:
- Combine all cities into a comma-separated string for each state.
- Only include states that have at least
3cities. - Only include states where at least one city starts with the same letter as the state name.
Return the result table ordered by the count of matching-letter cities in descending order and then by state name in ascending order.
The result format is in the following example.
Example:
Input:
cities table:
+--------------+---------------+
| state | city |
+--------------+---------------+
| New York | New York City |
| New York | Newark |
| New York | Buffalo |
| New York | Rochester |
| California | San Francisco |
| California | Sacramento |
| California | San Diego |
| California | Los Angeles |
| Texas | Tyler |
| Texas | Temple |
| Texas | Taylor |
| Texas | Dallas |
| Pennsylvania | Philadelphia |
| Pennsylvania | Pittsburgh |
| Pennsylvania | Pottstown |
+--------------+---------------+
Output:
+-------------+-------------------------------------------+-----------------------+
| state | cities | matching_letter_count |
+-------------+-------------------------------------------+-----------------------+
| Pennsylvania| Philadelphia, Pittsburgh, Pottstown | 3 |
| Texas | Dallas, Taylor, Temple, Tyler | 3 |
| New York | Buffalo, Newark, New York City, Rochester | 2 |
+-------------+-------------------------------------------+-----------------------+
Explanation:
- Pennsylvania:
- Has 3 cities (meets minimum requirement)
- All 3 cities start with 'P' (same as state)
- matching_letter_count = 3
- Texas:
- Has 4 cities (meets minimum requirement)
- 3 cities (Taylor, Temple, Tyler) start with 'T' (same as state)
- matching_letter_count = 3
- New York:
- Has 4 cities (meets minimum requirement)
- 2 cities (Newark, New York City) start with 'N' (same as state)
- matching_letter_count = 2
- California is not included in the output because:
- Although it has 4 cities (meets minimum requirement)
- No cities start with 'C' (doesn't meet the matching letter requirement)
Note:
- Results are ordered by matching_letter_count in descending order
- When matching_letter_count is the same (Texas and New York both have 2), they are ordered by state name alphabetically
- Cities in each row are ordered alphabetically
Solution
Approach: GROUP BY state with a conditional count
Group the rows by state and compute three things per group:
GROUP_CONCAT(city ORDER BY city SEPARATOR ', '), which builds the comma-separated list with the cities in alphabetical order (the sample output uses,as the separator);COUNT(*), the number of cities;SUM(LEFT(city, 1) = LEFT(state, 1)), the number of cities that start with the same letter as the state. The comparison is 1 or 0, so the sum counts the matches.
HAVING then keeps the states with at least 3 cities and at least one matching city. The final ORDER BY uses the matching count descending, then the state name. Here HAVING is needed rather than WHERE because both conditions depend on aggregates.
The Pandas version builds the same three columns with a named aggregation and filters and sorts the result.
Sorting by plain city places New York City before Newark, because a space sorts before a letter. The sample output lists them the other way round, which is the order that ORDER BY REPLACE(city, ' ', '') gives.
SELECT
state,
GROUP_CONCAT(city ORDER BY city SEPARATOR ', ') AS cities,
SUM(LEFT(city, 1) = LEFT(state, 1)) AS matching_letter_count
FROM cities
GROUP BY state
HAVING COUNT(*) >= 3
AND SUM(LEFT(city, 1) = LEFT(state, 1)) > 0
ORDER BY matching_letter_count DESC, state;