EchoDemo's Blogs

LeetCode 查找常用字符

给定仅有小写字母组成的字符串数组 A,返回列表中的每个字符串中都显示的全部字符(包括重复字符)组成的列表。例如,如果一个字符在每个字符串中出现 3 次,但不是 4 次,则需要在最终答案中包含该字符 3 次。

你可以按任意顺序返回答案。

示例 1:

输入:["bella","label","roller"]
输出:["e","l","l"]

示例 2:

输入:["cool","lock","cook"]
输出:["c","o"]

题解一(利用set和map):

@ApiOperation("LeetCode算法题测试")
@RequestMapping(value = "/algorithm", method = RequestMethod.POST)
@ResponseBody
public List<String> algorithm(String[] A) {
    // 存放第一个字符串的字符
    Set<Character> characterSet = new HashSet<>(A[0].length());
    // 存放第一个字符串字符出现的次数,初始值为1
    Map<Character, Integer> a0CharacterIntegerMap = new HashMap<>(A[0].length());
    // 将第一个字符串中字符装载进set和map
    for (int i = 0; i< A[0].length(); i++) {
        // 如果set里面已经有该字符,则第一个字符串map对应的字符出现次数加一
        if (characterSet.contains(A[0].charAt(i))) {
            a0CharacterIntegerMap.put(A[0].charAt(i), a0CharacterIntegerMap.get(A[0].charAt(i)) + 1);
        } else {
            // 如果没有,则将该字符都初始化进map和set
            characterSet.add(A[0].charAt(i));
            a0CharacterIntegerMap.put(A[0].charAt(i), 1);
        }
    }
    // 对剩下的字符串进行处理
    for (int i = 1; i < A.length; i++) {
        // 每个字符串字符的临时map,初始值为0
        Map<Character, Integer> tempCharacterIntegerMap = new HashMap<>(A[0].length());
        for (int j = 0; j< A[i].length(); j++) {
            if (!tempCharacterIntegerMap.containsKey(A[i].charAt(j))) {
                tempCharacterIntegerMap.put(A[i].charAt(j), 0);
            }
            if (characterSet.contains((A[i].charAt(j)))) {
                tempCharacterIntegerMap.put(A[i].charAt(j), tempCharacterIntegerMap.get(A[i].charAt(j)) + 1);
            }
        }
        // 将临时map和第一个字符串的map作比较并处理
        for (int j = 0; j < A[0].length(); j++) {
            // 如果临时map没有第一个字符串map里面的字符,则第一个字符串map的该字符出现次数置为0
            if (!tempCharacterIntegerMap.containsKey(A[0].charAt(j))) {
                a0CharacterIntegerMap.put(A[0].charAt(j), 0);
            } else {
                // 如果临时map有第一个字符串map里面的字符,且临时map出现的次数更少,则取更少次数的值
                if (a0CharacterIntegerMap
                        .get(A[0].charAt(j)) > tempCharacterIntegerMap.get(A[0].charAt(j))) {
                    a0CharacterIntegerMap.put(A[0].charAt(j), tempCharacterIntegerMap.get(A[0].charAt(j)));
                }
            }
        }
    }
    List<String> list = new ArrayList<>();
    // 遍历set,取第一个字符串字符对应的出现次数
    for (Character character : characterSet) {
        if (a0CharacterIntegerMap.get(character) > 0) {
            for (int i = 0; i < a0CharacterIntegerMap.get(character); i++) {
                list.add(String.valueOf(character));
            }
        }
    }
    return list;
}

题解二(抓住提示字符都是小写字母):

@ApiOperation("LeetCode算法题测试")
@RequestMapping(value = "/algorithm", method = RequestMethod.POST)
@ResponseBody
public List<String> algorithm(String[] A) {
    int[] a0 = new int[26];
    for (int i = 0; i < 26; i++) {
        a0[i] = Integer.MAX_VALUE;
    }
    for (String str : A) {
        // 初始化
        int[] temp = new int[26];
        for (int i = 0; i < 26; i++) {
            temp[i] = 0;
        }
        // 统计字符出现次数
        for (char c : str.toCharArray()) {
            temp[c - 'a']++;
        }
        for (int i = 0; i < 26; i++) {
            a0[i] = Math.min(a0[i], temp[i]);
        }
    }
    List<String> list = new ArrayList<>();
    for (int i = 0; i < 26; i++) {
        while (a0[i] > 0) {
            list.add(String.valueOf((char)('a' + i)));
            a0[i]--;
        }
    }
    return list;
}
🐶 您的支持将鼓励我继续创作 🐶
-------------本文结束感谢您的阅读-------------