|
46 | 46 | <!-- 这里可写当前语言的特殊实现逻辑 -->
|
47 | 47 |
|
48 | 48 | ```python
|
49 |
| - |
| 49 | +class Solution: |
| 50 | + def commonChars(self, words: List[str]) -> List[str]: |
| 51 | + freq = [10000] * 26 |
| 52 | + for word in words: |
| 53 | + t = [0] * 26 |
| 54 | + for c in word: |
| 55 | + t[ord(c) - ord('a')] += 1 |
| 56 | + for i in range(26): |
| 57 | + freq[i] = min(freq[i], t[i]) |
| 58 | + res = [] |
| 59 | + for i in range(26): |
| 60 | + if freq[i] > 0: |
| 61 | + res.extend([chr(i + ord("a"))] * freq[i]) |
| 62 | + return res |
50 | 63 | ```
|
51 | 64 |
|
52 | 65 | ### **Java**
|
53 | 66 |
|
54 | 67 | <!-- 这里可写当前语言的特殊实现逻辑 -->
|
55 | 68 |
|
56 | 69 | ```java
|
57 |
| - |
| 70 | +class Solution { |
| 71 | + public List<String> commonChars(String[] words) { |
| 72 | + int[] freq = new int[26]; |
| 73 | + Arrays.fill(freq, 10000); |
| 74 | + for (String word : words) { |
| 75 | + int[] t = new int[26]; |
| 76 | + for (char c : word.toCharArray()) { |
| 77 | + ++t[c - 'a']; |
| 78 | + } |
| 79 | + for (int i = 0; i < 26; ++i) { |
| 80 | + freq[i] = Math.min(freq[i], t[i]); |
| 81 | + } |
| 82 | + } |
| 83 | + List<String> res = new ArrayList<>(); |
| 84 | + for (int i = 0; i < 26; ++i) { |
| 85 | + while (freq[i]-- > 0) { |
| 86 | + res.add(String.valueOf((char) (i + 'a'))); |
| 87 | + } |
| 88 | + } |
| 89 | + return res; |
| 90 | + } |
| 91 | +} |
58 | 92 | ```
|
59 | 93 |
|
60 |
| -### **Go** |
| 94 | +### **C++** |
61 | 95 |
|
62 | 96 | ```go
|
63 |
| -func commonChars(A []string) []string { |
64 |
| - if len(A) == 0 { |
65 |
| - return []string{} |
66 |
| - } |
67 |
| - res := make([]int, 26) |
68 |
| - //以第一个字符串为基准,先统计出现次数 |
69 |
| - for _, c := range A[0] { |
70 |
| - res[c - 'a']++ |
71 |
| - } |
72 |
| - for i := 1; i < len(A); i++ { |
73 |
| - tmp := make([]int, 26) |
74 |
| - //统计后续每个字符串的字符出现次数 |
75 |
| - for _, c := range A[i] { |
76 |
| - tmp[c - 'a']++ |
| 97 | +class Solution { |
| 98 | +public: |
| 99 | + vector<string> commonChars(vector<string>& words) { |
| 100 | + vector<int> freq(26, 10000); |
| 101 | + for (auto word : words) |
| 102 | + { |
| 103 | + vector<int> t(26); |
| 104 | + for (char c : word) |
| 105 | + ++t[c - 'a']; |
| 106 | + for (int i = 0; i < 26; ++i) |
| 107 | + freq[i] = min(freq[i], t[i]); |
77 | 108 | }
|
78 |
| - //比较,取小 |
79 |
| - for j := 0; j < 26; j++ { |
80 |
| - res[j] = getMin(res[j], tmp[j]) |
| 109 | + vector<string> res; |
| 110 | + for (int i = 0; i < 26; i++) |
| 111 | + { |
| 112 | + while (freq[i]--) |
| 113 | + res.emplace_back(1, i + 'a'); |
81 | 114 | }
|
| 115 | + return res; |
82 | 116 | }
|
83 |
| - //遍历res,取出字符转换为string数组元素 |
84 |
| - result := make([]string,0) |
85 |
| - for i := 0; i < len(res); i++ { |
86 |
| - if res[i] > 0 { |
87 |
| - for j := 0; j < res[i]; j++ { |
88 |
| - result = append(result, string('a' + i)) |
89 |
| - } |
90 |
| - } |
91 |
| - } |
92 |
| - return result |
93 |
| -} |
| 117 | +}; |
| 118 | +``` |
94 | 119 |
|
95 |
| -func getMin(a,b int) int { |
96 |
| - if a > b{ |
97 |
| - return b |
98 |
| - } |
99 |
| - return a |
| 120 | +### **Go** |
| 121 | + |
| 122 | +```go |
| 123 | +func commonChars(words []string) []string { |
| 124 | + freq := make([]int, 26) |
| 125 | + for i := 0; i < 26; i++ { |
| 126 | + freq[i] = 10000 |
| 127 | + } |
| 128 | + for _, word := range words { |
| 129 | + t := make([]int, 26) |
| 130 | + for _, c := range word { |
| 131 | + t[c-'a']++ |
| 132 | + } |
| 133 | + for i := 0; i < 26; i++ { |
| 134 | + freq[i] = min(freq[i], t[i]) |
| 135 | + } |
| 136 | + } |
| 137 | + var res []string |
| 138 | + for i := 0; i < 26; i++ { |
| 139 | + for j := 0; j < freq[i]; j++ { |
| 140 | + res = append(res, string('a'+i)) |
| 141 | + } |
| 142 | + } |
| 143 | + return res |
100 | 144 | }
|
101 | 145 |
|
| 146 | +func min(a, b int) int { |
| 147 | + if a < b { |
| 148 | + return a |
| 149 | + } |
| 150 | + return b |
| 151 | +} |
102 | 152 | ```
|
103 | 153 |
|
104 | 154 | <!-- tabs:end -->
|
105 |
| -<!-- tabs:end --> |
|
0 commit comments