1002. Find Common Characters LeetCode Solution

In this guide, you will get 1002. Find Common Characters LeetCode Solution with the best time and space complexity. The solution to Find Common Characters 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

  1. Problem Statement
  2. Complexity Analysis
  3. Find Common Characters solution in C++
  4. Find Common Characters solution in Java
  5. Find Common Characters solution in Python
  6. Additional Resources
1002. Find Common Characters LeetCode Solution image

Problem Statement of Find Common Characters

Given a string array words, return an array of all characters that show up in all strings within the words (including duplicates). You may return the answer in any order.

Example 1:
Input: words = [“bella”,”label”,”roller”]
Output: [“e”,”l”,”l”]
Example 2:
Input: words = [“cool”,”lock”,”cook”]
Output: [“c”,”o”]

Constraints:

1 <= words.length <= 100
1 <= words[i].length <= 100
words[i] consists of lowercase English letters.

Complexity Analysis

  • Time Complexity: O(\Sigma |\texttt{words[i]}|)
  • Space Complexity: O(\max(|\texttt{words[i]}|))

1002. Find Common Characters LeetCode Solution in C++

class Solution {
 public:
  vector<string> commonChars(vector<string>& words) {
    vector<string> ans;
    vector<int> commonCount(26, INT_MAX);

    for (const string& word : words) {
      vector<int> count(26);
      for (const char c : word)
        ++count[c - 'a'];
      for (int i = 0; i < 26; ++i)
        commonCount[i] = min(commonCount[i], count[i]);
    }

    for (char c = 'a'; c <= 'z'; ++c)
      for (int i = 0; i < commonCount[c - 'a']; ++i)
        ans.push_back(string(1, c));

    return ans;
  }
};
/* code provided by PROGIEZ */

1002. Find Common Characters LeetCode Solution in Java

class Solution {
  public List<String> commonChars(String[] words) {
    List<String> ans = new ArrayList<>();
    int[] commonCount = new int[26];
    Arrays.fill(commonCount, Integer.MAX_VALUE);

    for (final String word : words) {
      int[] count = new int[26];
      for (final char c : word.toCharArray())
        ++count[c - 'a'];
      for (int i = 0; i < 26; ++i)
        commonCount[i] = Math.min(commonCount[i], count[i]);
    }

    for (char c = 'a'; c <= 'z'; ++c)
      for (int i = 0; i < commonCount[c - 'a']; ++i)
        ans.add(String.valueOf(c));

    return ans;
  }
}
// code provided by PROGIEZ

1002. Find Common Characters LeetCode Solution in Python

class Solution:
  def commonChars(self, words: list[str]) -> list[str]:
    return functools.reduce(lambda a, b: a & b,
                            map(collections.Counter, words)).elements()
# code by PROGIEZ

Additional Resources

See also  738. Monotone Increasing Digits LeetCode Solution

Happy Coding! Keep following PROGIEZ for more updates and solutions.