Skip to main content
  1. LeetCode/

LeetCode 3527: Find the Most Common Response

·1 min· ·
LeetCode Bi-Weekly Medium
Wei Yi Chung
Author
Wei Yi Chung
Working at the contributing of open source, distributed systems, and data engineering.
Table of Contents
LeetCode Bi-Weekly Contest 155 - This article is part of a series.
Part 1: This Article

Meta Data
#

Difficulty: medium
First Attempt: 2025-04-26

  • Total time: 10:00.00

Intuition
#

Use a Counter to count the occurrences of each response status.
Then, if there are multiple statuses with the same maximum occurrence,
select the lexicographically smallest one by taking the minimum among them.

Approach
#

class Solution:
    def findCommonResponse(self, responses: List[List[str]]) -> str:
        from collections import Counter
        l = []
        
        for response in responses:
            l.extend(list(set(response)))
            
        count_map = Counter(l)
        max_occur = max(count_map.values())
        
        status_list = [k for k,v in count_map.items() if v == max_occur]
        return sorted(status_list)[0]

Optimization Based on Runtime Ranking
#

We don’t need to create a list l and then update it with all sets of responses before using Counter.
Instead, we can initialize an empty Counter and directly update it with the set(response) during iteration.
Also, to get the lexicographically smallest status, we can simply use min().

class Solution:
    def findCommonResponse(self, responses: List[List[str]]) -> str:
        from collections import Counter
        
        counter = Counter()
        
        for response in responses:
            res = set(response)
            counter.update(res)
    
        max_occur = max(counter.values())
        
        status_list = [k for k,v in counter.items() if v == max_occur]
        return min(status_list)

Findings
#

N/A

Encountered Problems
#

N/A

LeetCode Bi-Weekly Contest 155 - This article is part of a series.
Part 1: This Article

Related

LeetCode 3528: Unit Conversion I
·1 min
LeetCode Bi-Weekly Medium
LeetCode Problem Solving
LeetCode 3513: Number of Unique XOR Triplets I
·2 mins
LeetCode Bi-Weekly Medium Unsolved Bit
LeetCode Problem Solving
LeetCode 2145: Count the Hidden Sequences
·2 mins
LeetCode Daily Medium
LeetCode Problem Solving
LeetCode 2799: Count Complete Subarrays in an Array
·1 min
LeetCode Daily Medium
LeetCode Problem Solving
LeetCode 2381: Shifting Letters II
·2 mins
LeetCode Medium Difference Array
LeetCode Problem Solving
LeetCode 3522: Calculate Score After Performing Instructions
·1 min
LeetCode Weekly Medium
LeetCode Problem Solving