leetcode 1002 查找共用字符

简介: leetcode 1002 查找共用字符

查找共用字符

class Solution {
public:
    vector<string> commonChars(vector<string>& words) {
        vector<string> result;
        vector<vector<int>> map(words.size() , vector<int>(26,0));
        for(int i=0 ; i<words.size() ;i++)
        {
            for(int j=0 ; j<words[i].size() ;j++)
                map[i][words[i][j] - 'a'] += 1;
        }
        for(int j=0 ; j<26 ;j++)
        {
            int tmp = INT_MAX;
            for(int i=0 ; i<words.size() ;i++)
            {
                if(map[i][j] == 0 ) break;
                tmp = min( tmp, map[i][j]);
                if(i==words.size()-1)
                {
                    while(tmp--)
                    {
                        string s(1,'a'+j);
                        result.push_back(s);
                    }
                } 
            }
        }
        return result;
    }
};
相关文章
|
2天前
|
存储 算法 程序员
【Leetcode 程序员面试金典 01.01】判定字符是否唯一 —— 位运算|哈希表
可以使用哈希表或位运算来解决此问题:由题可知s[i]仅包含小写字母,int[26]即能表示字符的出现次数;
|
2天前
|
存储 算法 Java
LeetCode 无重复字符的最长子串 打败100%的人
LeetCode 无重复字符的最长子串 打败100%的人
63 0
|
2天前
|
存储 索引
LeetCode 387. 字符串中的第一个唯一字符
LeetCode 387. 字符串中的第一个唯一字符
19 0
|
2天前
|
算法
LeetCode算法题---无重复字符的最长子串、寻找两个正序数组的中位数(三)
LeetCode算法题---无重复字符的最长子串、寻找两个正序数组的中位数(三)
41 0
|
2天前
|
Go
golang力扣leetcode 3.无重复字符的最长子串
golang力扣leetcode 3.无重复字符的最长子串
28 0
|
2天前
|
网络协议
《 LeetCode 热题 HOT 100》——无重复字符的最长子串
《 LeetCode 热题 HOT 100》——无重复字符的最长子串
|
2天前
|
存储 算法 Go
LeetCode 第三题: 无重复字符的最长子串
  给定一个字符串,请你找出其中不含有重复字符的最长子串的长度。
|
2天前
【Leetcode 2707】字符串中的额外字符 —— 动态规划
1. 状态定义:把`s[i−1]`当做是额外字符,`d[i] = d[i−1] + 1` 2. 状态转移方程:遍历所有的`j(j∈[0,i−1])`,如果子字符串`s[j...i−1]`存在于`dictionary`中,那么`d[i] = min d[j] 3. 初始状态`d[0] = 0`,最终答案为`d[n]`
|
2天前
leetcode:3. 无重复字符的最长子串
leetcode:3. 无重复字符的最长子串
17 0