File tree Expand file tree Collapse file tree 1 file changed +36
-0
lines changed Expand file tree Collapse file tree 1 file changed +36
-0
lines changed Original file line number Diff line number Diff line change 1+ // id: 1160
2+ // Name: Find Words That Can Be Formed by Characters
3+ // link: https://leetcode.com/problems/find-words-that-can-be-formed-by-characters/
4+ // Difficulty: Easy
5+
6+ class Solution {
7+ int [] map = new int [26 ];
8+ int [] wordMap ;
9+ public int countCharacters (String [] words , String chars ) {
10+ for (int i = 0 ; i < chars .length (); i ++) {
11+ map [chars .charAt (i )-'a' ]++;
12+ }
13+
14+ int result = 0 ;
15+ for (String word : words ) {
16+ if (matches (word )) {
17+ result += word .length ();
18+ }
19+ }
20+
21+
22+ return result ;
23+ }
24+
25+ private boolean matches (String word ) {
26+ wordMap = new int [26 ];
27+
28+ for (int i = 0 ; i < word .length (); i ++) {
29+ int c = word .charAt (i ) - 'a' ;
30+ wordMap [c ]++;
31+ if (wordMap [c ]>map [c ]) return false ;
32+ }
33+
34+ return true ;
35+ }
36+ }
You can’t perform that action at this time.
0 commit comments