/**
* 1002. Find Common Characters
* https://leetcode.com/problems/find-common-characters/submissions/
* Given an array A of strings made only from lowercase letters,
* return a list of all characters that show up in all strings within the list (including duplicates).
* For example, if a character occurs 3 times in all strings but not 4 times, you need to include that character three times in the final answer.
You may return the answer in any order.
Example 1:
Input: ["bella","label","roller"]
Output: ["e","l","l"]
* */
class Solution {
fun commonChars(A: Array<String>): List<String> {
val result = ArrayList<String>()
val map = HashMap<Char, IntArray>()
for (i in 0..(A.size - 1)) {
for (c in A[i]) {
if (!map.containsKey(c)) {
map.put(c, IntArray(A.size))
}
//array数组保存为:出现过的char在每一个A的子数组中出现的次数,顺序对应着A的子数组
//如a在"bella","label","roller",对应为[1,1,0]
val array = map.get(c)!!
array[i]++
map.put(c, array)
}
}
map.forEach { key, charCountArr ->
var canInsertFlag = 0
for (v in charCountArr) {
canInsertFlag += v
}
var canInsert = canInsertFlag >= A.size
if (canInsert) {
charCountArr.sort()
var insertCount = charCountArr[0]
for (j in 0..(insertCount - 1)) {
result.add(key.toString())
}
}
}
return result
}
}