class Solution:
def grandSlams(self, titles: List[str]) -> bool:
# The four Grand Slam tournaments
slams = {"Australian Open", "French Open", "Wimbledon", "US Open"}
# Use a set to store unique titles won
won = set(titles)
# Check if all slams are present
return slams.issubset(won)
class Solution {
public:
bool grandSlams(vector<string>& titles) {
unordered_set<string> slams = {"Australian Open", "French Open", "Wimbledon", "US Open"};
unordered_set<string> won(titles.begin(), titles.end());
for (const string& s : slams) {
if (won.find(s) == won.end()) return false;
}
return true;
}
};
class Solution {
public boolean grandSlams(List<String> titles) {
Set<String> slams = new HashSet<>(Arrays.asList("Australian Open", "French Open", "Wimbledon", "US Open"));
Set<String> won = new HashSet<>(titles);
return won.containsAll(slams);
}
}
function grandSlams(titles) {
const slams = new Set(["Australian Open", "French Open", "Wimbledon", "US Open"]);
const won = new Set(titles);
for (let slam of slams) {
if (!won.has(slam)) return false;
}
return true;
}
The Grand Slam Titles problem asks you to determine whether a tennis player has won all four major Grand Slam tournaments at least once. You are given a list of tournament titles the player has won, stored as strings in the list titles
. The four Grand Slam tournaments are:
Australian Open
French Open
Wimbledon
US Open
True
(or true
) if the player has won all four tournaments at least once, and False
otherwise. Each title in titles
can appear multiple times, but you only care about whether each Grand Slam has been won at least once.
Constraints:
When approaching this problem, the first idea is to check if all four required tournament names appear somewhere in the input list titles
. Since the list could contain duplicates or irrelevant tournaments, we need to filter out only the relevant Grand Slam titles.
A brute-force method would be to check for each of the four Grand Slam tournament names individually within the list. However, this could be repetitive and inefficient, especially if the list is long and contains many duplicates.
To optimize, we can use a set to store the titles the player has won. Sets automatically handle duplicates and allow for fast membership checks. By comparing the set of required Grand Slam tournaments with the set of titles won, we can efficiently determine if the player has achieved a Grand Slam.
We will solve the problem step by step:
{"Australian Open", "French Open", "Wimbledon", "US Open"}
.
titles
into a set. This automatically removes duplicates and allows for O(1) lookup times.
issubset
(Python), containsAll
(Java), or equivalent in other languages.
True
; otherwise, return False
.
This approach is efficient and concise, leveraging set properties for quick checks and easy code readability.
Example Input:
titles = ["Australian Open", "US Open", "Wimbledon", "French Open", "Australian Open"]
titles
to a set to remove duplicates:
won = {"Australian Open", "US Open", "Wimbledon", "French Open"}
slams = {"Australian Open", "French Open", "Wimbledon", "US Open"}
slams
is a subset of won
:
True
.
Example Input 2:
titles = ["Australian Open", "US Open", "Wimbledon"]
Here, "French Open"
is missing, so the answer is False
.
Brute-force Approach:
The optimized approach is both concise and efficient, making it suitable for large lists of titles.
The Grand Slam Titles problem is elegantly solved by leveraging set operations to check for the presence of all required tournaments. By converting the input list to a set, we remove duplicates and enable fast membership checks. This approach is efficient (O(N) time) and easy to understand, making it a robust solution for detecting whether a player has achieved a Grand Slam in tennis.