Java 代码优化:计算字符串数组中好字符串的总长度

本文将介绍如何优化一段 Java 代码,该代码用于计算字符串数组中满足特定条件的“好字符串”的总长度。

原始代码:

public static int countGoodStrings(String[] words, String chars) {
    Map<String[], String> charCount = new HashMap<>();
    for (char c : chars.toCharArray()) {
        charCount.put(c, charCount.getOrDefault(c, 0) + 1);
    }

    int totalCount = 0;

    for (String word : words) {
        HashMap<Character, Integer> wordsCount = new HashMap<>();
        boolean isGoodString = true;

        for (Character c : word.toCharArray()) {
            wordsCount.put(c, wordsCount.getOrDefault(c, 0) + 1);
            if (!charCount.containsKey(c) || charCount.get(c) < wordsCount.get(c)) {
                isGoodString = false;
                break;
            }
        }
        if(isGoodString){
            totalCount += word.length();
        }
    }
    return totalCount;
}

代码分析:

这段代码的功能是统计字符串数组 words 中所有满足以下条件的字符串的总长度:

  1. 字符串中的所有字符都必须出现在字符串 chars 中。
  2. 字符串中每个字符出现的次数不能超过 chars 中该字符出现的次数。

代码存在以下问题:

  1. Map<String[], String> charCount = new HashMap<>(); 这行代码的语法是错误的,应该将其修改为 Map<Character, Integer> charCount = new HashMap<>();,将String[]改为Character,因为我们要存储每个字符出现的次数。
  2. charCount.put(c, charCount.getOrDefault(c, 0) + 1); 这行代码的逻辑也有错误,应该将其修改为 charCount.put(c, charCount.getOrDefault(c, 0) + 1);,将charCount.getOrDefault(c, 0)改为charCount.getOrDefault(c, 0) + 1,因为我们要统计每个字符出现的次数。
  3. HashMap<Character, Integer> wordsCount = new HashMap<>(); 这行代码可以简化为 Map<Character, Integer> wordsCount = new HashMap<>();,不需要使用具体的实现类HashMap

代码修改:

public static int countGoodStrings(String[] words, String chars) {
    Map<Character, Integer> charCount = new HashMap<>();
    for (char c : chars.toCharArray()) {
        charCount.put(c, charCount.getOrDefault(c, 0) + 1);
    }

    int totalCount = 0;

    for (String word : words) {
        Map<Character, Integer> wordsCount = new HashMap<>();
        boolean isGoodString = true;

        for (char c : word.toCharArray()) {
            wordsCount.put(c, wordsCount.getOrDefault(c, 0) + 1);
            if (!charCount.containsKey(c) || charCount.get(c) < wordsCount.get(c)) {
                isGoodString = false;
                break;
            }
        }
        if(isGoodString){
            totalCount += word.length();
        }
    }
    return totalCount;
}

总结:

通过以上修改,我们修复了代码中的语法错误和逻辑错误,并简化了代码。修改后的代码能够准确地计算出字符串数组中所有满足条件的字符串的总长度。

优化后的代码实现:

public static int countGoodStrings(String[] words, String chars) {
    Map<Character, Integer> charCount = new HashMap<>();
    for (char c : chars.toCharArray()) {
        charCount.put(c, charCount.getOrDefault(c, 0) + 1);
    }

    int totalCount = 0;

    for (String word : words) {
        Map<Character, Integer> wordsCount = new HashMap<>();
        boolean isGoodString = true;

        for (char c : word.toCharArray()) {
            wordsCount.put(c, wordsCount.getOrDefault(c, 0) + 1);
            if (!charCount.containsKey(c) || charCount.get(c) < wordsCount.get(c)) {
                isGoodString = false;
                break;
            }
        }
        if (isGoodString) {
            totalCount += word.length();
        }
    }
    return totalCount;
}

这段代码使用了一个 HashMap 来存储字符 chars 中每个字符出现的次数。然后,对于每个字符串 word,代码使用另一个 HashMap 来存储该字符串中每个字符出现的次数。最后,代码检查 word 中每个字符是否在 chars 中,以及出现的次数是否超过 chars 中该字符出现的次数。如果满足条件,则该字符串是“好字符串”,将该字符串的长度累加到 totalCount 中。

通过以上优化,代码更加简洁明了,并且易于理解和维护。

Java 代码优化:计算字符串数组中好字符串的总长度

原文地址: https://www.cveoy.top/t/topic/p7q3 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录