返回介绍

solution / 1000-1099 / 1002.Find Common Characters / README_EN

发布于 2024-06-17 01:03:32 字数 3864 浏览 0 评论 0 收藏 0

1002. Find Common Characters

中文文档

Description

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.

Solutions

Solution 1

class Solution:
  def commonChars(self, words: List[str]) -> List[str]:
    cnt = Counter(words[0])
    for w in words:
      ccnt = Counter(w)
      for c in cnt.keys():
        cnt[c] = min(cnt[c], ccnt[c])
    ans = []
    for c, v in cnt.items():
      ans.extend([c] * v)
    return ans
class Solution {
  public List<String> commonChars(String[] words) {
    int[] cnt = new int[26];
    Arrays.fill(cnt, 10000);
    for (String w : words) {
      int[] ccnt = new int[26];
      for (int i = 0; i < w.length(); ++i) {
        ++ccnt[w.charAt(i) - 'a'];
      }
      for (int i = 0; i < 26; ++i) {
        cnt[i] = Math.min(cnt[i], ccnt[i]);
      }
    }
    List<String> ans = new ArrayList<>();
    for (int i = 0; i < 26; ++i) {
      while (cnt[i]-- > 0) {
        ans.add(String.valueOf((char) (i + 'a')));
      }
    }
    return ans;
  }
}
class Solution {
public:
  vector<string> commonChars(vector<string>& words) {
    int cnt[26];
    memset(cnt, 0x3f, sizeof(cnt));
    for (auto& w : words) {
      int ccnt[26]{};
      for (char& c : w) {
        ++ccnt[c - 'a'];
      }
      for (int i = 0; i < 26; ++i) {
        cnt[i] = min(cnt[i], ccnt[i]);
      }
    }
    vector<string> ans;
    for (int i = 0; i < 26; ++i) {
      while (cnt[i]--) {
        ans.emplace_back(1, i + 'a');
      }
    }
    return ans;
  }
};
func commonChars(words []string) (ans []string) {
  cnt := [26]int{}
  for i := range cnt {
    cnt[i] = 1 << 30
  }
  for _, w := range words {
    ccnt := [26]int{}
    for _, c := range w {
      ccnt[c-'a']++
    }
    for i, v := range cnt {
      cnt[i] = min(v, ccnt[i])
    }
  }
  for i, v := range cnt {
    for v > 0 {
      ans = append(ans, string(i+'a'))
      v--
    }
  }
  return
}
function commonChars(words: string[]): string[] {
  const freq: number[] = new Array(26).fill(10000);
  for (const word of words) {
    const t: number[] = new Array(26).fill(0);
    for (const c of word.split('')) {
      ++t[c.charCodeAt(0) - 'a'.charCodeAt(0)];
    }
    for (let i = 0; i < 26; ++i) {
      freq[i] = Math.min(freq[i], t[i]);
    }
  }
  const res: string[] = [];
  for (let i = 0; i < 26; ++i) {
    while (freq[i]-- > 0) {
      res.push(String.fromCharCode(i + 'a'.charCodeAt(0)));
    }
  }
  return res;
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文