Want Help Cracking FAANG?

(Then click this)

×
Back to Question Bank

1783. Grand Slam Titles - Leetcode Solution

Code Implementation

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;
}
      

Problem Description

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
Your task is to return 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:

  • There is only one valid answer for each input.
  • Do not reuse elements—each title only counts for the tournament it represents.
  • The input list may contain other non-Grand Slam titles, but only the four listed above are required for a "Grand Slam".

Thought Process

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.

Solution Approach

We will solve the problem step by step:

  1. Define the Set of Grand Slam Tournaments: Create a set containing the four required tournament names: {"Australian Open", "French Open", "Wimbledon", "US Open"}.
  2. Collect Titles Won: Convert the input list titles into a set. This automatically removes duplicates and allows for O(1) lookup times.
  3. Check for Grand Slam: Use set operations to check if all four required tournaments are present in the set of titles won. This can be done with the issubset (Python), containsAll (Java), or equivalent in other languages.
  4. Return Result: If all four Grand Slam tournaments are present, return True; otherwise, return False.

This approach is efficient and concise, leveraging set properties for quick checks and easy code readability.

Example Walkthrough

Example Input:
titles = ["Australian Open", "US Open", "Wimbledon", "French Open", "Australian Open"]

  1. Convert titles to a set to remove duplicates:
    won = {"Australian Open", "US Open", "Wimbledon", "French Open"}
  2. Define the Grand Slam set:
    slams = {"Australian Open", "French Open", "Wimbledon", "US Open"}
  3. Check if slams is a subset of won:
    Since all four are present, the answer is True.

Example Input 2:
titles = ["Australian Open", "US Open", "Wimbledon"]
Here, "French Open" is missing, so the answer is False.

Time and Space Complexity

Brute-force Approach:

  • For each of the four Grand Slam tournaments, iterate through the input list to check if it's present.
  • Time Complexity: O(4N) = O(N), where N is the number of titles in the input list.
  • Space Complexity: O(1), not counting input storage.
Optimized (Set-based) Approach:
  • Converting the list to a set takes O(N) time.
  • Checking if the Grand Slam set is a subset is O(1), since the set size is constant (4).
  • Time Complexity: O(N)
  • Space Complexity: O(N) for the set of titles won.

The optimized approach is both concise and efficient, making it suitable for large lists of titles.

Summary

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.