Untitled

mail@pastecode.io avatar
unknown
java
a year ago
3.2 kB
3
Indexable
Never
import java.io.*;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.core.JsonProcessingException;

/*
NaCl -> sodium chloride (salt) -> Na + Cl
H2O -> water -> 2*H + O
C6H12O6 -> glucose -> 6*C + 12*H + 6*O

countOfElements("NaCl") // expect {"Na": 1, "Cl": 1}
countOfElements("H2O") // expect {"H": 2, "O": 1}
countOfElements("C6H12O6") // expect {"C": 6, "H": 12, "O", 6}

*/

// Main class should be named 'Solution' and should not be public.

import java.util.*;

class Solution {
    public static void main(String[] args) {
        System.out.println(countOfElements("NaCl"));
        System.out.println(countOfElements("H2O"));
        System.out.println(countOfElements("C6H12O6"));
    }
    
    
    private static Map<String, Integer> countOfElements(String str) {
        if (str == null || str.isEmpty()) {
            return null;
        }
        
        
        List<String> stringList = new ArrayList<String>();
        
        int len = str.length();
        int startPos = 0;
        
        for (int i = 0; i < len; i++) {
            char c = str.charAt(i);
            if (i == len - 1) {
                if (Character.isUpperCase(str.charAt(i))) {
                    String firstElement = str.substring(startPos, len - 1);
                    String secondElement = str.charAt(i) + "";
                    stringList.add(firstElement);
                    stringList.add(secondElement);
                } else {
                    String s = str.substring(startPos, len);
                    startPos = i;
                    stringList.add(s);
                }
               
            } else if (Character.isUpperCase(c) && i > 0) {
                String s = str.substring(startPos, i);
                startPos = i;
                stringList.add(s);
                continue;
            }
            
        }
        
        return printFrequency(stringList);
    }
    
    private static Map<String, Integer> printFrequency(List<String> stringList) {
        Map<String, Integer> map = new HashMap();
        
       // System.out.print("In Frequencey method");
        
        for(String str: stringList) {
            
            String stringRes = "";
            int idx = 0;
            
            
            while (idx < str.length() && !Character.isDigit(str.charAt(idx))) {
              //  System.out.print("String for string separation");
                stringRes += str.charAt(idx);
                idx++;
            }
            
            int res = 0;
            
            while (idx < str.length() && Character.isDigit(str.charAt(idx))) {
               // System.out.print("Looping for digit separation");
                int dig = Character.getNumericValue(str.charAt(idx));
                res = res * 10 + dig;
                idx++;
            }
            
            if (res == 0) {
                map.put(stringRes, 1);
            } else {
                 map.put(stringRes, res);
            }
            
        }
        
        return map;
    }
}