Skip to content

Commit 30caeb7

Browse files
committedApr 5, 2025
Add solution #1160
1 parent 5de0a31 commit 30caeb7

File tree

2 files changed

+44
-1
lines changed

2 files changed

+44
-1
lines changed
 

‎README.md

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# 1,158 LeetCode solutions in JavaScript
1+
# 1,159 LeetCode solutions in JavaScript
22

33
[https://leetcodejavascript.com](https://leetcodejavascript.com)
44

@@ -906,6 +906,7 @@
906906
1155|[Number of Dice Rolls With Target Sum](./solutions/1155-number-of-dice-rolls-with-target-sum.js)|Medium|
907907
1156|[Swap For Longest Repeated Character Substring](./solutions/1156-swap-for-longest-repeated-character-substring.js)|Medium|
908908
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|
909910
1161|[Maximum Level Sum of a Binary Tree](./solutions/1161-maximum-level-sum-of-a-binary-tree.js)|Medium|
910911
1189|[Maximum Number of Balloons](./solutions/1189-maximum-number-of-balloons.js)|Easy|
911912
1200|[Minimum Absolute Difference](./solutions/1200-minimum-absolute-difference.js)|Easy|
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
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+
};

0 commit comments

Comments
 (0)
Please sign in to comment.