3527. Find the Most Common Response LeetCode Solution
In this guide, you will get 3527. Find the Most Common Response LeetCode Solution with the best time and space complexity. The solution to Find the Most Common Response problem is provided in various programming languages like C++, Java, and Python. This will be helpful for you if you are preparing for placements, hackathons, interviews, or practice purposes. The solutions provided here are very easy to follow and include detailed explanations.
Table of Contents
- Problem Statement
- Complexity Analysis
- Find the Most Common Response solution in C++
- Find the Most Common Response solution in Java
- Find the Most Common Response solution in Python
- Additional Resources
Problem Statement of Find the Most Common Response
You are given a 2D string array responses where each responses[i] is an array of strings representing survey responses from the ith day.
Return the most common response across all days after removing duplicate responses within each responses[i]. If there is a tie, return the lexicographically smallest response.
Example 1:
Input: responses = [[“good”,”ok”,”good”,”ok”],[“ok”,”bad”,”good”,”ok”,”ok”],[“good”],[“bad”]]
Output: “good”
Explanation:
After removing duplicates within each list, responses = [[“good”, “ok”], [“ok”, “bad”, “good”], [“good”], [“bad”]].
“good” appears 3 times, “ok” appears 2 times, and “bad” appears 2 times.
Return “good” because it has the highest frequency.
Example 2:
Input: responses = [[“good”,”ok”,”good”],[“ok”,”bad”],[“bad”,”notsure”],[“great”,”good”]]
Output: “bad”
Explanation:
After removing duplicates within each list we have responses = [[“good”, “ok”], [“ok”, “bad”], [“bad”, “notsure”], [“great”, “good”]].
“bad”, “good”, and “ok” each occur 2 times.
The output is “bad” because it is the lexicographically smallest amongst the words with the highest frequency.
Constraints:
1 <= responses.length <= 1000
1 <= responses[i].length <= 1000
1 <= responses[i][j].length <= 10
responses[i][j] consists of only lowercase English letters
Complexity Analysis
- Time Complexity: O(\Sigma |\texttt{responses[i]}|)
- Space Complexity: O(\Sigma |\texttt{responses[i]}|)
3527. Find the Most Common Response LeetCode Solution in C++
class Solution {
public:
string findCommonResponse(vector<vector<string>>& responses) {
string ans;
unordered_map<string, int> count;
int maxFreq = 0;
for (const vector<string>& response : responses)
for (const string& response :
unordered_set<string>{response.begin(), response.end()})
++count[response];
for (const pair<const string, int>& entry : count)
maxFreq = max(maxFreq, entry.second);
for (const auto& [response, freq] : count)
if (freq == maxFreq && (ans.empty() || response < ans))
ans = response;
return ans;
}
};
/* code provided by PROGIEZ */
3527. Find the Most Common Response LeetCode Solution in Java
class Solution {
public String findCommonResponse(List<List<String>> responses) {
String ans = "";
Map<String, Integer> count = new HashMap<>();
int maxFreq = 0;
for (List<String> response : responses)
for (final String r : new HashSet<>(response))
count.merge(r, 1, Integer::sum);
for (final int freq : count.values())
maxFreq = Math.max(maxFreq, freq);
for (Map.Entry<String, Integer> entry : count.entrySet()) {
final String response = entry.getKey();
final int freq = entry.getValue();
if (freq == maxFreq && (ans.isEmpty() || response.compareTo(ans) < 0))
ans = response;
}
return ans;
}
}
// code provided by PROGIEZ
3527. Find the Most Common Response LeetCode Solution in Python
class Solution:
def findCommonResponse(self, responses: list[list[str]]) -> str:
count = collections.Counter()
for response in responses:
for response in set(response):
count[response] += 1
maxFreq = max(count.values())
return min([response
for response, count in count.items()
if count == maxFreq])
# code by PROGIEZ
Additional Resources
- Explore all LeetCode problem solutions at Progiez here
- Explore all problems on LeetCode website here
Happy Coding! Keep following PROGIEZ for more updates and solutions.