|
| 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 | +} |
0 commit comments