Java solution, Stack + Hashmap


  • 0
    W
    class Solution {
        public String countOfAtoms(String formula) {
            HashMap<String, Integer> bank = new HashMap<>(),temp;
            Stack<HashMap<String, Integer>> stack = new Stack<>();
            stack.push(bank);
            char cha;
            int base = 1;
            String name,num;
            int intnum;
            for(int i=0; i< formula.length();i++){
                cha = formula.charAt(i);
                if(cha == '('){
                    stack.push(new HashMap<String, Integer>());
                }
                if(cha == ')'){
                    temp = stack.pop();
                    
                    if(i+1<formula.length()&&Character.isDigit(formula.charAt(i+1))){
                        i++;
                        num="";
                        num+=formula.charAt(i);
                        while(i+1<formula.length()&&Character.isDigit(formula.charAt(i+1))){
                            i++;
                            cha = formula.charAt(i);
                            num+=cha;
                        }
                        intnum = Integer.parseInt(num);
                    }else{
                        intnum = 1;
                    }
                    for(String str: temp.keySet()){
                        stack.peek().put(str,stack.peek().getOrDefault(str,0)+temp.get(str)*intnum);
                    }
                }
                if(Character.isUpperCase(cha)){
                    name="";
                    name+=cha;
                    while(i+1<formula.length()&&Character.isLowerCase(formula.charAt(i+1))){
                        i++;
                        cha = formula.charAt(i);
                        name+=cha;
                    }
                    
                    if(i+1<formula.length()&&Character.isDigit(formula.charAt(i+1))){
                        i++;
                        num="";
                        num+=formula.charAt(i);
                        while(i+1<formula.length()&&Character.isDigit(formula.charAt(i+1))){
                            i++;
                            cha = formula.charAt(i);
                            num+=cha;
                        }
                        intnum = Integer.parseInt(num);
                    }else{
                        intnum = 1;
                    }
                    stack.peek().put(name,stack.peek().getOrDefault(name,0)+intnum);
                }
            }
            String[] strs = new String[bank.keySet().size()];
            int ind =0;
            for(String str:bank.keySet()){
                strs[ind]=str;
                ind++;
            }
            Arrays.sort(strs);
            StringBuilder sb = new StringBuilder();
            int numsssss;
            for(String str: strs){
                numsssss = bank.get(str);
                sb.append(str);
                if(numsssss>1){
                    sb.append(numsssss);
                }
            }
            return sb.toString();
        }
    }
    

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.