File tree 2 files changed +44
-1
lines changed
2 files changed +44
-1
lines changed Original file line number Diff line number Diff line change 1
- # 1,158 LeetCode solutions in JavaScript
1
+ # 1,159 LeetCode solutions in JavaScript
2
2
3
3
[ https://leetcodejavascript.com ] ( https://leetcodejavascript.com )
4
4
906
906
1155|[ Number of Dice Rolls With Target Sum] ( ./solutions/1155-number-of-dice-rolls-with-target-sum.js ) |Medium|
907
907
1156|[ Swap For Longest Repeated Character Substring] ( ./solutions/1156-swap-for-longest-repeated-character-substring.js ) |Medium|
908
908
1157|[ Online Majority Element In Subarray] ( ./solutions/1157-online-majority-element-in-subarray.js ) |Hard|
909
+ 1160|[ Find Words That Can Be Formed by Characters] ( ./solutions/1160-find-words-that-can-be-formed-by-characters.js ) |Easy|
909
910
1161|[ Maximum Level Sum of a Binary Tree] ( ./solutions/1161-maximum-level-sum-of-a-binary-tree.js ) |Medium|
910
911
1189|[ Maximum Number of Balloons] ( ./solutions/1189-maximum-number-of-balloons.js ) |Easy|
911
912
1200|[ Minimum Absolute Difference] ( ./solutions/1200-minimum-absolute-difference.js ) |Easy|
Original file line number Diff line number Diff line change
1
+ /**
2
+ * 1160. Find Words That Can Be Formed by Characters
3
+ * https://leetcode.com/problems/find-words-that-can-be-formed-by-characters/
4
+ * Difficulty: Easy
5
+ *
6
+ * You are given an array of strings words and a string chars.
7
+ *
8
+ * A string is good if it can be formed by characters from chars (each character can only be
9
+ * used once).
10
+ *
11
+ * Return the sum of lengths of all good strings in words.
12
+ */
13
+
14
+ /**
15
+ * @param {string[] } words
16
+ * @param {string } chars
17
+ * @return {number }
18
+ */
19
+ var countCharacters = function ( words , availableChars ) {
20
+ const charFrequency = new Map ( ) ;
21
+ for ( const char of availableChars ) {
22
+ charFrequency . set ( char , ( charFrequency . get ( char ) || 0 ) + 1 ) ;
23
+ }
24
+
25
+ let result = 0 ;
26
+ for ( const word of words ) {
27
+ const tempFrequency = new Map ( charFrequency ) ;
28
+ let isValid = true ;
29
+
30
+ for ( const char of word ) {
31
+ if ( ! tempFrequency . has ( char ) || tempFrequency . get ( char ) === 0 ) {
32
+ isValid = false ;
33
+ break ;
34
+ }
35
+ tempFrequency . set ( char , tempFrequency . get ( char ) - 1 ) ;
36
+ }
37
+
38
+ if ( isValid ) result += word . length ;
39
+ }
40
+
41
+ return result ;
42
+ } ;
You can’t perform that action at this time.
0 commit comments