Find Words That Can Be Formed by Characters
给一个字符串组和一个字符串s, 问s里面的字符能组成几个字符串组里的字符串. 返回这些字符串的长度和.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
class Solution { public int countCharacters(String[] words, String chars) { int res = 0; int[] c = count(chars); for(String s : words) { int[] t = count(s); for(int i = 0; i <= 26; i++) { if(i == 26){ res += s.length(); break; } if(t[i] > c[i]) break; } } return res; } private int[] count(String s) { int[] count = new int[26]; for(char c : s.toCharArray()) count[c - 'a'] ++; return count; } } |