Skip to content

Commit c4688c8

Browse files
solves find words that can be formed by characters
1 parent 2a68a8f commit c4688c8

File tree

2 files changed

+35
-3
lines changed

2 files changed

+35
-3
lines changed

README.md

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
# LeetCode Algorithms
22

3-
![problems-solved](https://img.shields.io/badge/Problems%20Solved-231/2081-1f425f.svg)
4-
![problems-solved-java](https://img.shields.io/badge/Java-231/2081-1abc9c.svg)
3+
![problems-solved](https://img.shields.io/badge/Problems%20Solved-248/2081-1f425f.svg)
4+
![problems-solved-java](https://img.shields.io/badge/Java-248/2081-1abc9c.svg)
55
![problems-solved-python](https://img.shields.io/badge/Python-186/2081-1abc9c.svg)
66
[![PRs Welcome](https://img.shields.io/badge/PRs-welcome-brightgreen.svg)](CONTRIBUTING.md)
77
[![cp](https://img.shields.io/badge/also%20see-Competitve%20Programming-1f72ff.svg)](https://github.com/anishLearnsToCode/competitive-programming)
@@ -307,7 +307,7 @@
307307
| 1150 | [Check if Number is Majority Element in Sorted Array](https://leetcode.com/problems/check-if-a-number-is-majority-element-in-a-sorted-array) | | |
308308
| 1154 | [Day of The Year](https://leetcode.com/problems/day-of-the-year) | [![Java](assets/java.png)](src/DayOfTheYear.java) | |
309309
| 1160 | [Find Words That Can Be Formed By Characters](https://leetcode.com/problems/find-words-that-can-be-formed-by-characters) | | |
310-
| 1165 | [Single Row Keyboard](https://leetcode.com/problems/single-row-keyboard) | | |
310+
| 1165 | [Single Row Keyboard](https://leetcode.com/problems/single-row-keyboard) | [![Java](assets/java.png)](src/FindWordsThatCanBeFormedByCharacters.java) | |
311311
| 1170 | [Compare Strings By Frequency of the Smallest Character](https://leetcode.com/problems/compare-strings-by-frequency-of-the-smallest-character) | | |
312312
| 1175 | [Prime Arrangements](https://leetcode.com/problems/prime-arrangements) | | |
313313
| 1176 | [Diet Plan Performance](https://leetcode.com/problems/diet-plan-performance) | | |
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
import java.util.HashMap;
2+
import java.util.Map;
3+
4+
public class FindWordsThatCanBeFormedByCharacters {
5+
public int countCharacters(String[] words, String chars) {
6+
final Map<Character, Integer> characterFrequencies = getCharFrequencies(chars);
7+
int result = 0;
8+
for (String word : words) {
9+
if (word.length() > chars.length()) continue;
10+
Map<Character, Integer> wordCharFrequencies = getCharFrequencies(word);
11+
if (canBeFormedBy(wordCharFrequencies, characterFrequencies)) {
12+
result += word.length();
13+
}
14+
}
15+
return result;
16+
}
17+
18+
private Map<Character, Integer> getCharFrequencies(String string) {
19+
Map<Character, Integer> frequencies = new HashMap<>();
20+
for (int index = 0 ; index < string.length() ; index++) {
21+
frequencies.put(string.charAt(index), frequencies.getOrDefault(string.charAt(index), 0) + 1);
22+
}
23+
return frequencies;
24+
}
25+
26+
private boolean canBeFormedBy(Map<Character, Integer> frequencies1, Map<Character, Integer> frequencies2) {
27+
for (Map.Entry<Character, Integer> entry : frequencies1.entrySet()) {
28+
if (frequencies2.getOrDefault(entry.getKey(), 0) < entry.getValue()) return false;
29+
}
30+
return true;
31+
}
32+
}

0 commit comments

Comments
 (0)