zoukankan      html  css  js  c++  java
  • 1002. Find Common Characters

    /**
     * 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
        }
    }
  • 相关阅读:
    算法
    ximalaya-spider
    无名小站
    python send email
    spider-bilibili
    windows镜像
    python 爬取豆瓣电影写入到excel中
    pdf 转 word
    文档分割、合并
    文档合并
  • 原文地址:https://www.cnblogs.com/johnnyzhao/p/10604957.html
Copyright © 2011-2022 走看看