Skip to content

Commit 56045dd

Browse files
add 3992
1 parent 893dd51 commit 56045dd

2 files changed

Lines changed: 34 additions & 0 deletions

File tree

  • paginated_contents/algorithms/4th_thousand
  • src/main/java/com/fishercoder/solutions/fourththousand

paginated_contents/algorithms/4th_thousand/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
| # | Title | Solutions | Video | Difficulty | Tag
22
|------|---------------------------------------------------------------------------------------------------------------------------------------------------------------------|-----------------------------------------------------------------------------------------------------------------------------------|-------------------------------------------------------------------------------|------------|----------------------------------------------------------------------
33
| 3997 | [Count Dominant Nodes in a Binary Tree](https://leetcode.com/problems/count-dominant-nodes-in-a-binary-tree/) | [Java](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/fourththousand/_3997.java) | | Medium |
4+
| 3992 | [Rearrange String to Avoid Character Pair](https://leetcode.com/problems/rearrange-string-to-avoid-character-pair/) | [Java](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/fourththousand/_3992.java) | | Easy |
45
| 3982 | [Sum of Integers with Maximum Digit Range](https://leetcode.com/problems/sum-of-integers-with-maximum-digit-range/) | [Java](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/fourththousand/_3982.java) | | Easy |
56
| 3908 | [Valid Digit Number](https://leetcode.com/problems/valid-digit-number/) | [Java](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/fourththousand/_3908.java) | | Easy |
67
| 3502 | [Minimum Cost to Reach Every Position](https://leetcode.com/problems/minimum-cost-to-reach-every-position/) | [Java](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/fourththousand/_3502.java) | | Easy |
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package com.fishercoder.solutions.fourththousand;
2+
3+
import java.util.HashMap;
4+
import java.util.Map;
5+
6+
public class _3992 {
7+
public static class Solution1 {
8+
public String rearrangeString(String s, char x, char y) {
9+
Map<Character, Integer> map = new HashMap<>();
10+
for (char c : s.toCharArray()) {
11+
map.put(c, map.getOrDefault(c, 0) + 1);
12+
}
13+
StringBuilder sb = new StringBuilder();
14+
Integer count = map.getOrDefault(y, 0);
15+
while (count-- > 0) {
16+
sb.append(y);
17+
}
18+
map.remove(y);
19+
count = map.getOrDefault(x, 0);
20+
while (count-- > 0) {
21+
sb.append(x);
22+
}
23+
map.remove(x);
24+
for (Map.Entry<Character, Integer> entry : map.entrySet()) {
25+
count = entry.getValue();
26+
while (count-- > 0) {
27+
sb.append(entry.getKey());
28+
}
29+
}
30+
return sb.toString();
31+
}
32+
}
33+
}

0 commit comments

Comments
 (0)