Leetcode--1160.拼写单词(Java)
发布日期:2021-04-30 21:05:45 浏览次数:78 分类:精选文章

本文共 1468 字,大约阅读时间需要 4 分钟。

?????????????????????????????????????????????????????????????????????????????????????????????????????????

????

  • ????????????????????????????????????26?????
  • ?????????????????????????????????????????????????????????????????
  • ????????????????????????????????????
  • ????

    public class Solution {
    public int countCharacters(String[] words, String chars) {
    if (words.length == 0 || chars.length() == 0) {
    return 0;
    }
    int[] arr = new int[26];
    for (int i = 0; i < chars.length(); i++) {
    char c = chars.charAt(i);
    arr[c - 'a']++;
    }
    int count = 0;
    for (int i = 0; i < words.length; i++) {
    String word = words[i];
    int[] temp = new int[26];
    boolean canForm = true;
    for (int j = 0; j < word.length(); j++) {
    char c = word.charAt(j);
    int index = c - 'a';
    if (temp[index] >= arr[index]) {
    canForm = false;
    break;
    }
    temp[index]++;
    }
    if (canForm) {
    count += word.length();
    }
    }
    return count;
    }
    }

    ????

  • ????????????????26???arr?????????????????????????
  • ??????????????????????temp?????????????????????????????????????????????
  • ???????????????????????????????????????????????????????????
  • ??????????????????????????????????
  • ?????????????????????????????????????

    上一篇:Leetcode--424. 替换后的最长重复字符
    下一篇:Leetcode--416. 分割等和子集

    发表评论

    最新留言

    感谢大佬
    [***.8.128.20]2026年05月26日 12时12分42秒