Skip to content

Commit 4ab2386

Browse files
add 2423
1 parent 84fdd1f commit 4ab2386

File tree

3 files changed

+114
-0
lines changed
  • paginated_contents/algorithms/3rd_thousand
  • src

3 files changed

+114
-0
lines changed

Diff for: paginated_contents/algorithms/3rd_thousand/README.md

+1
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@
5454
| 2433 | [Find The Original Array of Prefix Xor](https://leetcode.com/problems/find-the-original-array-of-prefix-xor/) | [Java](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/thirdthousand/_2433.java) | [:tv:](https://youtu.be/idcT-p_DDrI) | Medium ||
5555
| 2432 | [The Employee That Worked on the Longest Task](https://leetcode.com/problems/the-employee-that-worked-on-the-longest-task/) | [Java](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/thirdthousand/_2432.java) || Easy ||
5656
| 2427 | [Number of Common Factors](https://leetcode.com/problems/number-of-common-factors/) | [Java](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/thirdthousand/_2427.java) || Easy ||
57+
| 2423 | [Remove Letter To Equalize Frequency](https://leetcode.com/problems/remove-letter-to-equalize-frequency/) | [Java](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/thirdthousand/_2423.java) || Easy ||
5758
| 2418 | [Sort the People](https://leetcode.com/problems/sort-the-people/) | [Java](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/thirdthousand/_2418.java) || Easy ||
5859
| 2413 | [Smallest Even Multiple](https://leetcode.com/problems/smallest-even-multiple/) | [Java](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/thirdthousand/_2413.java) || Easy ||
5960
| 2409 | [Count Days Spent Together](https://leetcode.com/problems/count-days-spent-together/) | [Java](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/thirdthousand/_2409.java) || Easy ||
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
package com.fishercoder.solutions.thirdthousand;
2+
3+
import java.util.Arrays;
4+
5+
public class _2423 {
6+
public static class Solution1 {
7+
/**
8+
* This is my original, but unnecessarily verbose solution.
9+
* Instead, you can just brute force each one of the 26 letters, as long as any one of them makes it meet the requirement, it returns true.
10+
*/
11+
public boolean equalFrequency(String word) {
12+
int[] count = new int[26];
13+
for (char c : word.toCharArray()) {
14+
count[c - 'a']++;
15+
}
16+
Arrays.sort(count);
17+
return decLast(count) || decFirst(count) || allOnes(count);
18+
}
19+
20+
private boolean allOnes(int[] count) {
21+
for (int i : count) {
22+
if (i != 1 && i != 0) {
23+
return false;
24+
}
25+
}
26+
return true;
27+
}
28+
29+
private boolean decFirst(int[] count) {
30+
int start = 0;
31+
int firstVal = -1;
32+
for (int i = 0; i < 26; i++) {
33+
if (count[i] != 0) {
34+
start = i + 1;
35+
firstVal = count[i] - 1;
36+
break;
37+
}
38+
}
39+
if (firstVal == 0) {
40+
int nextVal = count[start++];
41+
for (; start < 26; start++) {
42+
if (count[start] != nextVal) {
43+
return false;
44+
}
45+
}
46+
return true;
47+
}
48+
for (; start < 26; start++) {
49+
if (count[start] != firstVal) {
50+
return false;
51+
}
52+
}
53+
return true;
54+
}
55+
56+
private boolean decLast(int[] count) {
57+
int last = count[25] - 1;
58+
for (int i = 24; i >= 0; i--) {
59+
if (count[i] != 0 && count[i] != last) {
60+
return false;
61+
}
62+
}
63+
return true;
64+
}
65+
}
66+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
package com.fishercoder.thirdthousand;
2+
3+
import com.fishercoder.solutions.thirdthousand._2423;
4+
import org.junit.jupiter.api.BeforeEach;
5+
import org.junit.jupiter.api.Test;
6+
7+
import static org.junit.jupiter.api.Assertions.assertFalse;
8+
import static org.junit.jupiter.api.Assertions.assertTrue;
9+
10+
public class _2423Test {
11+
private static _2423.Solution1 solution1;
12+
13+
@BeforeEach
14+
public void setup() {
15+
solution1 = new _2423.Solution1();
16+
}
17+
18+
@Test
19+
public void test1() {
20+
assertTrue(solution1.equalFrequency("abcc"));
21+
}
22+
23+
@Test
24+
public void test2() {
25+
assertTrue(solution1.equalFrequency("abc"));
26+
}
27+
28+
@Test
29+
public void test3() {
30+
assertFalse(solution1.equalFrequency("ddaccb"));
31+
}
32+
33+
@Test
34+
public void test4() {
35+
assertTrue(solution1.equalFrequency("abbcc"));
36+
}
37+
38+
@Test
39+
public void test5() {
40+
assertFalse(solution1.equalFrequency("aazz"));
41+
}
42+
43+
@Test
44+
public void test6() {
45+
assertTrue(solution1.equalFrequency("ab"));
46+
}
47+
}

0 commit comments

Comments
 (0)