Skip to content

Commit 174ecf4

Browse files
solves redistribute characters to make all strings equal
1 parent c25ffb1 commit 174ecf4

File tree

2 files changed

+28
-1
lines changed

2 files changed

+28
-1
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -457,7 +457,7 @@
457457
| 1880 | [Check if Word Equals Summation of Two Words](https://leetcode.com/problems/check-if-word-equals-summation-of-two-words) | [![Java](assets/java.png)](src/CheckIfWordEqualsSummationOfTwoWords.java) | |
458458
| 1886 | [Determine Whether Matrix Can Be Obtained By Rotation](https://leetcode.com/problems/determine-whether-matrix-can-be-obtained-by-rotation) | [![Java](assets/java.png)](src/DetermineWhetherMatrixCanBeObtainedByRotation.java) | |
459459
| 1893 | [Check if All the Integers in a Range Are Covered](https://leetcode.com/problems/check-if-all-the-integers-in-a-range-are-covered) | [![Java](assets/java.png)](src/CheckIfAllTheIntegersInARangeAreCovered.java) | |
460-
| 1897 | [Redistribute Characters to Make All Strings Equal](https://leetcode.com/problems/redistribute-characters-to-make-all-strings-equal) | | |
460+
| 1897 | [Redistribute Characters to Make All Strings Equal](https://leetcode.com/problems/redistribute-characters-to-make-all-strings-equal) | [![Java](assets/java.png)](src/RedistributeCharactersToMakeAllStringsEqual.java) | |
461461
| 1903 | [Largest Odd Number in String](https://leetcode.com/problems/largest-odd-number-in-string) | | |
462462
| 1909 | [Remove One Element to Make the Array Strictly Increasing](https://leetcode.com/problems/remove-one-element-to-make-the-array-strictly-increasing) | | |
463463
| 1913 | [Maximum Product Difference Between Two Pairs](https://leetcode.com/problems/maximum-product-difference-between-two-pairs) | | |
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
// https://leetcode.com/problems/redistribute-characters-to-make-all-strings-equal
2+
// T: O(|words| * |word|)
3+
// S: O(1)
4+
5+
import java.util.HashMap;
6+
import java.util.Map;
7+
8+
public class RedistributeCharactersToMakeAllStringsEqual {
9+
public boolean makeEqual(String[] words) {
10+
if (words.length == 1) return true;
11+
final Map<Character, Integer> frequencies = getCharacterFrequencies(words);
12+
for (int frequency : frequencies.values()) {
13+
if (frequency % words.length != 0) return false;
14+
}
15+
return true;
16+
}
17+
18+
private Map<Character, Integer> getCharacterFrequencies(String[] words) {
19+
final Map<Character, Integer> frequencies = new HashMap<>();
20+
for (String word : words) {
21+
for (int i = 0 ; i < word.length() ; i++) {
22+
frequencies.put(word.charAt(i), frequencies.getOrDefault(word.charAt(i), 0) + 1);
23+
}
24+
}
25+
return frequencies;
26+
}
27+
}

0 commit comments

Comments
 (0)