-
-
Notifications
You must be signed in to change notification settings - Fork 609
/
Copy pathNumberOfAtoms.java
76 lines (64 loc) · 2.37 KB
/
NumberOfAtoms.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
package problems.hard;
import java.util.*;
import java.util.stream.Collectors;
/**
* Why Did you create this class? what does it do?
*/
public class NumberOfAtoms {
// public static void main(String[] args) {
// System.out.println(countOfAtoms("K4(ON(SO3)2)2"));
// }
// 2(2(3OS)NO)4K
static public String countOfAtoms(String formula) {
if (formula == null || formula.length() == 0)
return "";
StringBuilder builder = new StringBuilder(formula).reverse();
Map<String, Integer> map = new HashMap<>();
Stack<Integer> stack = new Stack<>();
String elem = "";
int sum = 1;
for (int i = 0; i < builder.length(); i++) {
char c = builder.charAt(i);
if (Character.isDigit(c)) {
int j = i;
while (j < builder.length() && Character.isDigit(builder.charAt(j)))
j++;
if (j != i) {
stack.add(Integer.parseInt(builder.substring(i, j)));
sum *= stack.peek();
j = i = 1;
}
} else if (c == '(') {
}
}
return map.keySet().stream().sorted().map(s -> s + map.get(s)).collect(Collectors.joining());
}
private static int getNextDigit(StringBuilder builder, int i) {
return 1;
}
// public int shoppingOffers(List<Integer> price, List<List<Integer>> special, List<Integer> needs) {
// if (price.size() == 0 || needs.size() == 0)
// return 0;
// int actual = 0;
// for (int i = 0; i < needs.size(); i++) {
// actual += needs.get(i) * price.get(i);
// }
// int mini = -1;
// int result = 0;
// for (int i = 0; i < special.size(); i++) {
// List<Integer> offer = special.get(i);
// int offerCost = 0;
// int actualCost = 0;
// for (int j = 0; j < needs.size(); j++) {
// if (offer.get(i) > needs.get(i)) {
// offerCost = 0;
// break;
// }
// offerCost += offer.get(i) * needs.get(i);
// actualCost += price.get(i) * needs.get(i);
// }
//
// }
// return result;
// }
}