Skip to content

Commit 6f0918c

Browse files
committedJul 14, 2024·
add 726
1 parent fe72929 commit 6f0918c

File tree

3 files changed

+144
-0
lines changed
  • paginated_contents/algorithms/1st_thousand
  • src
    • main/java/com/fishercoder/solutions/firstthousand
    • test/java/com/fishercoder/firstthousand

3 files changed

+144
-0
lines changed
 

‎paginated_contents/algorithms/1st_thousand/README.md

+1
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,7 @@
145145
| 729 | [My Calendar I](https://leetcode.com/problems/my-calendar-i/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/_729.java) || Medium |
146146
| 728 | [Self Dividing Numbers](https://leetcode.com/problems/self-dividing-numbers/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/_728.java) | | Easy |
147147
| 727 | [Minimum Window Subsequence](https://leetcode.com/problems/minimum-window-subsequence/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/_727.java) | | Hard | DP
148+
| 726 | [Number of Atoms](https://leetcode.com/problems/number-of-atoms/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/_726.java) | | Hard | Stack
148149
| 725 | [Split Linked List in Parts](https://leetcode.com/problems/split-linked-list-in-parts/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/_725.java) | | Medium | LinkedList
149150
| 724 | [Find Pivot Index](https://leetcode.com/problems/find-pivot-index/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/_724.java) | | Easy | Array
150151
| 723 | [Candy Crush](https://leetcode.com/problems/candy-crush/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/_723.java) | | Medium | Array, Two Pointers
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
package com.fishercoder.solutions.firstthousand;
2+
3+
import java.util.ArrayList;
4+
import java.util.Collections;
5+
import java.util.Deque;
6+
import java.util.HashMap;
7+
import java.util.LinkedList;
8+
import java.util.List;
9+
import java.util.Map;
10+
11+
public class _726 {
12+
public static class Solution1 {
13+
/**
14+
* My completely original solution:
15+
* 1. use a stack;
16+
* 2. whenever we encounter the open paren, we push it into the top of the stack;
17+
* 3. whenever we encounter an uppercase, we check to get its full atom letters,
18+
* then check to get the number after it if there's any, then form a pair objet and push onto the stack;
19+
* 4. whenever we encounter the closed paren, we check if there's any number after it,
20+
* then poll all items on top of the stack off onto a new/temp stack until we encounter the corresponding open paren,
21+
* then add these items from the temp stack back into the original/main stack;
22+
*/
23+
public String countOfAtoms(String formula) {
24+
Deque<Pair> stack = new LinkedList<>();
25+
for (int i = 0; i < formula.length(); i++) {
26+
char curr = formula.charAt(i);
27+
if (curr == '(') {
28+
stack.addLast(new Pair("(", 1));
29+
} else if (Character.isUpperCase(curr)) {
30+
StringBuilder sb = new StringBuilder(curr + "");
31+
i++;
32+
while (i < formula.length() && Character.isLowerCase(formula.charAt(i))) {
33+
sb.append(formula.charAt(i++));
34+
}
35+
if (i < formula.length()) {
36+
if (Character.isUpperCase(formula.charAt(i)) || formula.charAt(i) == '(' || formula.charAt(i) == ')') {
37+
//no numbers
38+
stack.addLast(new Pair(sb.toString(), 1));
39+
i--;
40+
} else {
41+
//there are numbers
42+
StringBuilder numberSb = new StringBuilder();
43+
while (i < formula.length() && Character.isDigit(formula.charAt(i))) {
44+
numberSb.append(formula.charAt(i++));
45+
}
46+
i--;
47+
stack.addLast(new Pair(sb.toString(), Integer.parseInt(numberSb.toString())));
48+
}
49+
} else {
50+
stack.addLast(new Pair(sb.toString(), 1));
51+
}
52+
} else if (curr == ')') {
53+
i++;
54+
StringBuilder sb = new StringBuilder();
55+
while (i < formula.length() && Character.isDigit(formula.charAt(i))) {
56+
sb.append(formula.charAt(i));
57+
i++;
58+
}
59+
i--;
60+
int number = 1;
61+
if (sb.length() > 0) {
62+
number = Integer.parseInt(sb.toString());
63+
}
64+
Deque<Pair> stack2 = new LinkedList<>();
65+
while (!stack.isEmpty() && !stack.peekLast().atom.equals("(")) {
66+
Pair pair = stack.pollLast();
67+
stack2.addLast(new Pair(pair.atom, pair.count * number));
68+
}
69+
stack.pollLast();//poll "(" off of the stack
70+
while (!stack2.isEmpty()) {
71+
stack.addLast(stack2.pollLast());
72+
}
73+
}
74+
}
75+
List<Pair> list = new ArrayList<>();
76+
while (!stack.isEmpty()) {
77+
list.add(stack.pollLast());
78+
}
79+
//now merge the same atoms
80+
Map<String, Integer> map = new HashMap<>();
81+
for (Pair pair : list) {
82+
map.put(pair.atom, map.getOrDefault(pair.atom, 0) + pair.count);
83+
}
84+
//now add the merged atoms into the list again before sorting them
85+
list.clear();
86+
for (Map.Entry<String, Integer> entry : map.entrySet()) {
87+
list.add(new Pair(entry.getKey(), entry.getValue()));
88+
}
89+
Collections.sort(list, (a, b) -> a.atom.compareToIgnoreCase(b.atom));
90+
StringBuilder sb = new StringBuilder();
91+
for (Pair pair : list) {
92+
sb.append(pair.atom + (pair.count == 1 ? "" : pair.count));
93+
}
94+
return sb.toString();
95+
}
96+
97+
class Pair {
98+
String atom;
99+
int count;
100+
101+
public Pair(String atom, int count) {
102+
this.atom = atom;
103+
this.count = count;
104+
}
105+
}
106+
}
107+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package com.fishercoder.firstthousand;
2+
3+
import com.fishercoder.solutions.firstthousand._726;
4+
import org.junit.jupiter.api.BeforeEach;
5+
import org.junit.jupiter.api.Test;
6+
7+
import static org.junit.jupiter.api.Assertions.assertEquals;
8+
9+
public class _726Test {
10+
private static _726.Solution1 solution1;
11+
12+
@BeforeEach
13+
public void setup() {
14+
solution1 = new _726.Solution1();
15+
}
16+
17+
@Test
18+
public void test1() {
19+
assertEquals("H2O", solution1.countOfAtoms("H2O"));
20+
}
21+
22+
@Test
23+
public void test2() {
24+
assertEquals("H2MgO2", solution1.countOfAtoms("Mg(OH)2"));
25+
}
26+
27+
@Test
28+
public void test3() {
29+
assertEquals("K4N2O14S4", solution1.countOfAtoms("K4(ON(SO3)2)2"));
30+
}
31+
32+
@Test
33+
public void test4() {
34+
assertEquals("H2MgNO", solution1.countOfAtoms("Mg(H2O)N"));
35+
}
36+
}

0 commit comments

Comments
 (0)
Please sign in to comment.